title: spring hibernate many-to-many mapping form input
body: <p>I need help with my many-to-many mapping, I want to add a list if items, into a class via a form. I am working on a dabase for movies and actors, and in the 'Movie' class I have a list that contains the actors in that movie, the list is mapped to the 'Actor' class.</p>
  
  <p>This is for adding a movie:</p>
  
  <pre><code>&lt;body&gt;
  Add Movie
  &lt;form:form modelAttribute="movie"&gt;
  Title:
  &lt;form:input path="title" /&gt;&lt;br /&gt;
  Rating:
  &lt;form:input path="rating" /&gt;&lt;br /&gt;
  Year:
  &lt;form:input path="year" /&gt;&lt;br /&gt;
  
  &lt;form:select path="actors"&gt;
      &lt;c:forEach items="${actor}" var="p"&gt;
          &lt;form:option value="${???}"&gt;${p.name}&lt;/form:option&gt;
      &lt;/c:forEach&gt;
  &lt;/form:select&gt;
  
  &lt;input type="submit" value="Save movie" /&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p>First I enter the values for the new movie (title, rating and the year it was released),  then I have a select form, wich lists a set of actors, the thought is to select what actors that are in the movie, but I don't know how to do it.</p>
  
  <p>Controller:</p>
  
  <pre><code>    @RequestMapping(value="/add", method = RequestMethod.GET)
  public Movie showMAddForm(Model model){
      List&lt;Actor&gt; 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";
  }
  </code></pre>
  
  <p>What do I have to do, to be able to select what actors I want in the newly created movie?</p>
  
  <p>EDIT: This is my Actor class</p>
  
  <pre><code>@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&lt;Movie&gt; movies = new ArrayList&lt;Movie&gt;();
  
  // Setters and Getters
  
  @ManyToMany(fetch = FetchType.EAGER, mappedBy = "actors")
  public List&lt;Movie&gt; getMovies(){
      return this.movies;
  }
  
  public void setMovies(List&lt;Movie&gt; movies){
      this.movies = movies;
  }
  
  }
  </code></pre>
  
  <p>And this is the Movie class, so you can see how the mapping is</p>
  
  <pre><code>@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&lt;Actor&gt; actors = new ArrayList&lt;Actor&gt;();
  
  // 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&lt;Actor&gt; getActors(){
      return this.actors;
  }
  public void setActors(List&lt;Actor&gt; actors){
      this.actors = actors;
  }
  
  }
  </code></pre>