我需要帮助我的多对多映射,我想通过表单将项目列表添加到类中。我正在制作电影和演员的数据库,以及电影'电影' class I有一个包含该电影中的actor的列表,该列表被映射到' Actor'类。
这是为了添加电影:
<body>
Add Movie
<form:form modelAttribute="movie">
Title:
<form:input path="title" /><br />
Rating:
<form:input path="rating" /><br />
Year:
<form:input path="year" /><br />
<form:select path="actors">
<c:forEach items="${actor}" var="p">
<form:option value="${???}">${p.name}</form:option>
</c:forEach>
</form:select>
<input type="submit" value="Save movie" />
首先我输入新电影的值(标题,评级和发布的年份),然后我有一个选择表格,列出一组演员,想法是选择电影中的演员,但我不知道该怎么做。
控制器:
@RequestMapping(value="/add", method = RequestMethod.GET)
public Movie showMAddForm(Model model){
List<Actor> actor = actorService.list();
model.addAttribute("actor", actor);
return new Movie();
}
@RequestMapping(value="/add", method = RequestMethod.POST)
public String saveMAddForm(Movie m){
movieService.saveMovie(m);
return "saved";
}
我需要做什么才能在新制作的电影中选择我想要的演员?
编辑:这是我的演员类
@Entity
@Table
public class Actor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int actor_id;
private String name;
private String age;
private String link;
private List<Movie> movies = new ArrayList<Movie>();
// Setters and Getters
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "actors")
public List<Movie> getMovies(){
return this.movies;
}
public void setMovies(List<Movie> movies){
this.movies = movies;
}
}
这是Movie类,所以你可以看到映射是如何
@Entity
@Table
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int movie_id;
private String title;
private String rating;
private String year;
private List<Actor> actors = new ArrayList<Actor>();
// Setters and getters
@ManyToMany(fetch = FetchType.EAGER, targetEntity = com.springapp.mvc.Actor.class)
@JoinTable(name= "ActorList", joinColumns = {@JoinColumn(name="actor_id")},
inverseJoinColumns= {@JoinColumn(name="movie_id")})
public List<Actor> getActors(){
return this.actors;
}
public void setActors(List<Actor> actors){
this.actors = actors;
}
}
答案 0 :(得分:1)
我不知道你是否确实需要这个,但我正在尝试,所以如果有任何问题,请告诉我,我会根据你的要求进行编辑:
person model class:Person.java
@Entity
@Table(name="person")
public class Person implements UserDetails{
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "person_seq_gen")
@SequenceGenerator(name = "person_seq_gen",sequenceName = "person_seq")
private int id;
@Valid
@NotEmpty @Email(message = "email.empty")
@Column(name = "username")
private String username;
@Valid
@Size(min = 6)
@NotEmpty(message = "Password may not be empty")
@Column(name = "password")
private String password;
@OneToMany(mappedBy = "person1",fetch = FetchType.EAGER)
private Set<Canvas> canvas1 = new HashSet<Canvas>();
public Set<Canvas> getCanvas1() {
return canvas1;
}
public void setCanvas1(Set<Canvas> canvas1) {
this.canvas1 = canvas1;
}
}
@Entity
@Table(name = "canvas")
public class Canvas {
@Id
@Column(name="canvasid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "canvas_gen")
@SequenceGenerator(name = "canvas_gen",sequenceName = "canvas_seq")
private int canvasid;
@Column(name = "canvasname")
private String canvasname;
@ManyToOne
@JoinColumn(name = "id",nullable = false)
private Person person1;
public Person getPerson1() {
return person1;
}
public void setPerson1(Person person1) {
this.person1 = person1;
}
public int getPerson1id(){
return this.person1.getId();
}
}
PersonDAOImpl:此类将人员保存在数据库中:
@Transactional
@Repository
public class PersonDAOImpl implements PersonDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
private Session session;
public void setSession(Session session){this.session = session;}
@Override
@Transactional
public void addPerson(Person p) {
if( session == null) {
session = this.sessionFactory.openSession();
}else {
session = this.sessionFactory.getCurrentSession();
}
session.saveOrUpdate(p);
}
}
CanvasDAOImpl:此类保存画布
@Transactional
@Repository
public class CanvasDAOImpl implements CanvasDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
private Session session;
public void setSession(Session session){this.session = session;}
@PreAuthorize("hasRole('ROLE_USER')")
@Override
public void addCanvas(Canvas canvas, Person person) {
int id = person.getId();
if( session == null) {
session = this.sessionFactory.openSession();
}else {
session = this.sessionFactory.getCurrentSession();
}
Person person1 = (Person) session.get(Person.class,id);
person.getCanvas1().add(canvas);
canvas.setPerson1(person1);
session.merge(canvas);
session.flush();
}
}
控制器方法:
@RequestMapping(value= "/person/add", method = RequestMethod.POST)
public String addPerson(@Valid Person person,BindingResult bindingResult,@ModelAttribute("person") Person p,Model model){
if(bindingResult.hasErrors()){
model.addAttribute("errors",bindingResult.getAllErrors());
return "redirect:/";
}
this.personService.addPerson(p);
return "redirect:/";
}
@RequestMapping("/canvas/add")
public String addCanvas(@ModelAttribute("canvases") Canvas canvas,Model model){
The method below uses Spring Security to find out who is logged in.
Person person = personService.getCurrentlyAuthenticatedUser();
model.addAttribute("canvases",new Canvas());
model.addAttribute("listCanvas", this.canvasService.listCanvases());
model.addAttribute("listCanvases",this.canvasService.listCanvasPerson(person));
this.canvasService.addCanvas(canvas,person);
return "redirect:/canvas/list";
}