2017-06-02 57 views
-1

Theese新行是豆類:春季啓動創建,而不是更新

@Entity 
@Table(name = "bands") 
public class Band implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
public Integer band_id; 

@Column(name = "name") 
@NotEmpty 
public String name; 

@Column(name = "formed") 
@NotNull 
public Integer formed; 

@ManyToOne 
@JoinColumn(name = "genre_id") 
public Genre genre; 

public Integer getBandId() { 
    return band_id; 
} 

public void setBandId(Integer band_id) { 
    this.band_id = band_id; 
} 

public String getName() { 
    return this.name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Integer getFormed() { 
    return this.formed; 
} 

public void setFormed(Integer formed) { 
    this.formed = formed; 
} 

public Genre getGenre() { 
    return genre; 
} 

public void setGenre(Genre genre) { 
    this.genre = genre; 
} 

@XmlElement 
public Genre getGenres() { 
    Genre genre = getGenre(); 
    return genre; 
} 

} 



@Entity 
@Table(name = "genres") 
@Access(AccessType.FIELD) 
public class Genre implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
public Integer genre_id; 

@Column(name = "name") 
@NotEmpty 
public String name; 

@OneToMany(cascade = CascadeType.ALL, mappedBy = "genre") 
public Set<Band> bands; 

public Integer getGenreId() { 
    return genre_id; 
} 

public void setGenreId(Integer genre_id) { 
    this.genre_id = genre_id; 
} 

public String getName() { 
    return this.name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Set<Band> getBands() { 
    return this.bands; 
} 

public void setBands(Set<Band> bands) { 
    this.bands = bands; 
} 

} 

這在inits頁面控制器部分,然後調用保存操作:

@RequestMapping(value = "/bands/{band_id}/edit", method = RequestMethod.GET) 
public String initUpdateBandForm(@PathVariable("band_id") int band_id, ModelMap model) { 
    model.addAttribute("genres", this.bandRepository.findAllGenres()); 
    Band band = this.bandRepository.findById(band_id); 
    model.addAttribute("band", band); 
    return "bands/updateBandForm"; 
} 

@RequestMapping(value = "bands/{band_id}/edit", method = RequestMethod.POST) 
public String processUpdateBandForm(@Valid @ModelAttribute("band") Band band, BindingResult result) { 
    if (result.hasErrors()) { 
     return "bands/updateBandForm"; 
    } else { 
     this.bandRepository.save(band); 
     return "redirect:/bands/{band_id}"; 
    } 
} 

這是存儲庫保存操作:

void save(Band band); 

這是updateBandForm:

<form th:object="${band}" method="post"> 

    <label>Name</label> 
    <input type="text" th:field="*{name}" /> 

    <label>Formed</label> 
    <input type="text" th:field="*{formed}" /> 

    <label>Genre</label> 

    <select th:field="*{genre}"> 
     <option th:each="genre: ${genres}" th:value="${{genre}}" th:text="${genre.name}" /> 
    </select> 

    <br> 
    <button type="submit">Update Band</button> 

</form> 

我也使用格式化:

@Service 
public class GenreFormatter implements Formatter<Genre> { 

@Autowired 
GenreRepository genreRepository; 

@Override 
public String print(Genre genre, Locale locale) { 
    return (genre != null ? genre.getGenreId().toString() : ""); 
} 

@Override 
public Genre parse(String text, Locale locale) throws ParseException { 
    Integer id = Integer.valueOf(text); 
    return this.genreRepository.findById(id); 
} 

} 

@Configuration 
@EnableWebMvc 
@ComponentScan(value = {"org.springframework.samples.discography.system"}) 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Autowired 
private GenreFormatter genreFormatter; 

@Override 
public void addFormatters(FormatterRegistry registry) { 
    registry.addFormatter(genreFormatter); 
} 

} 

控制器方法創建一個新的行,而不是更新現有的......能有人幫忙嗎? 我錯過了什麼?

回答

0

您的HTML表單沒有ID信息。

@ModelAttribute將創建一個Band對象,其中包含由HTML表單發送的請求參數中的數據。

由於您的表格只有nameformedgenreBand對象內部processUpdateBandForm有一個初始化band_id場,從而在創作上saveBand

在表單中添加ID信息以解決此問題。

<input type="hidden" th:field="*{bandId}" /> 
+0

謝謝您的回答,可惜我現在收到此錯誤:org.springframework.beans.NotReadablePropertyException:bean類[org.springframework.samples.discography.band.Band]的無效屬性 'band_id':豆屬性'band_id'不可讀或具有無效的getter方法:getter的返回類型是否與setter的參數類型匹配? –

+0

你的'Band' Java bean getter和setter是錯誤的。將字段更改爲'bandId'或將您的getter/setter修改爲'getBand_id'和'setBand_id'(如果您使用後者,則將輸入更改爲Thymeleaf中的「* {band_id}」')。 – kagmole

+0

謝謝!拿我的錢!有效! –

0

在您的processUpdateBandForm函數中,首先需要先從Db中獲取band對象,然後進行更新。現在你只是添加一個新的對象。所以框架認爲它是一個新的對象。

public String processUpdateBandForm(@Valid @ModelAttribute("band") Band band, BindingResult result) { 

    Band bandFromDb = this.bandRepository.findById(band.getBandId); 
    //compare band and bandFromDb and update fields from band to bandFromDb. 
    //Dont change the id field which is band_id(this will create new object) 
    this.bandRepository.save(bandFromDb) 
} 
+0

這裏的問題是band.getBandId()返回null。 –

+0

你是否正確,因爲Id是在這種情況下自動生成的。所以在這種情況下,您需要使用其他一些獨特的密鑰來搜索數據庫。 – pvpkiran