title: Spring MVC with Hibernate - OneToMany mapping in form
body: <p>I have Car and Rental models conntected OneToMany relation:</p>
  
  <pre><code>@Entity
  public class Rental {
  
  private Long id;
  private Car car;
  private User user;
  
  @Id
  @GeneratedValue
  @Column(name = "RENTAL_ID")
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "CAR_ID")
  public Car getCar() {
      return car;
  }
  
  public void setCar(Car car) {
      this.car = car;
  }
  
  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "USER_ID")
  public User getUser() {
      return user;
  }
  
  public void setUser(User user) {
      this.user = user;
  }
  }
  
  
  
  
  
  @Entity
  public class Car {
  
  private Long id;
  private String name;
  private String description;
  
  @Id
  @GeneratedValue
  @Column(name = "CAR_ID")
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  ...
  }
  </code></pre>
  
  <p>And I want to set car during rental creation:</p>
  
  <pre><code>&lt;form:form method="POST" commandName="rental"&gt;
  &lt;form:errors path="*" cssClass="errorblock" element="div" /&gt;
  &lt;table cellspacing="10"&gt;
      &lt;tr&gt;
          &lt;td&gt;Car&lt;/td&gt;
          &lt;td&gt;
          &lt;form:select path="car" multiple="false" items="${cars}" itemLabel="name" itemValue="id"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td colspan="2"&gt;&lt;input type="submit" value="Create"&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p></p>
  
  <p>Form:select tag displays correctly available cars, but after submit Car instance variable of Rental is null. </p>
  
  <pre><code>@Controller
  @RequestMapping("/make-rental.htm")
  public class MakeRentalController {
  
  private CarDAO carDAO;
  private UserDAO userDAO;
  private RentalDAO rentalDAO;
  
  @Autowired
  public void setCarDAO(CarDAO carDAO) {
      this.carDAO = carDAO;
  }
  
  @Autowired
  public void setUserDAO(UserDAO userDAO) {
      this.userDAO = userDAO;
  }
  
  @Autowired
  public void setRentalDAO(RentalDAO rentalDAO) {
      this.rentalDAO = rentalDAO;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String initForm(ModelMap mapModel) {
      Rental rental = new Rental();
  
      // command object
      mapModel.addAttribute("cars", carDAO.getAll());
      mapModel.addAttribute("rental", rental);
  
      // return form view
      return "makeRentalFormView";
  }
  
  @RequestMapping(method = RequestMethod.POST)
  public String processSubmit(@ModelAttribute("rental") Rental rental,
          BindingResult result, SessionStatus status, HttpSession session) {
      Object userId = session.getAttribute("userId");
      if (userId != null) {
          rental.setUser(userDAO.getById((Long) userId));
          rentalDAO.save(rental);
  
          // clear the command object from the session
          status.setComplete();
      }
  
      // form success
      return "redirect:/index.htm";
  }
  }
  </code></pre>
  
  <p>What's wrong?</p>