给出两个实体,电影和导演。
@实体
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>
这是没有 Director 处理的原始控制器
@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";
}
提交电影时加载现有 Director 实体的最佳模式是什么?非常感谢