給出兩個實體Movie和Director。使用Spring MVC提交數據時加載Hibernate實體的最佳實踐
@Entity
public class Movie extends BaseEntity {
private static final long serialVersionUID = -1545404740388837658L;
@Column(name="movieName", nullable=false, length=200)
private String movieName;
@Column(name="year", nullable=false)
private Integer year;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="director_id")
private Director director;
//setter, getter
}
@Entity
public class Director extends BaseEntity {
@NotEmpty
@Column(nullable = false, length=50)
private String name;
//setter, getter
}
並且在JSP形式
<form:form action="saveMovie" commandName="movie" >
<p><label>Movie Name</label><form:input path="movieName" /></p>
<p><label>Year</label><form:input path="year" /></p>
<p><label>Director</label><form:input id="txtDirector" path="director.id" /></p>
<p>
<input type="submit" name="Save" />
</p>
</form:form>
這是一個沒有董事原始控制器處理
@RequestMapping(value="/saveMovie", method = RequestMethod.POST)
public String saveMovie(@ModelAttribute Movie movie, BindingResult result, SessionStatus status, ModelMap model) {
getMovieModel().saveMovie(movie); //get a DAO to persist a Movie
List<Movie> movies = getMovieModel().getAllMovies(); //reload all Movies
model.put("allMovies", movies);
return "test/allMovies";
}
什麼是加載一個最好的模式現有的導演實體何時提交電影? 非常感謝
請更具體一些。你到底想要什麼? –
Hi Japs,如果Moive是一個新插入的實體,但Director已經存在於數據庫中(Director在客戶端視圖頁面中選擇),我如何確保Director將被更新而不是插入新的實體? – Matt