JPA 未将外键保存到@OneToMany关系
我正在使用Spring和Hibernate作为JPA提供商,并试图获得一个@OneToMany(具有许多电话号码的联系人)以将外键保存在电话号码表中。从我的表单中,我得到一个联系人对象,其中包含电话(号码)列表。联系人正确持久化(休眠从指定的序列中提取 PK)。电话(号码)列表也保留正确的PK,但“联系人”表没有FK。
public class Contact implements Serializable {
@OneToMany(mappedBy = "contactId", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Phone> phoneList;
}
public class Phone implements Serializable {
@JoinColumn(name = "contact_id", referencedColumnName = "contact_id")
@ManyToOne
private Contact contactId;
}
@Repository("contactDao")
@Transactional(readOnly = true)
public class ContactDaoImpl implements ContactDao {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void save(Contact c) {
em.persist(c);
em.flush();
}
}
@Controller
public class ContactController {
@RequestMapping(value = "/contact/new", method = RequestMethod.POST)
public ModelAndView newContact(Contact c) {
ModelAndView mv = new ModelAndView("contactForm");
contactDao.save(c);
mv.addObject("contact", c);
return mv;
}
}
希望我得到了上面的所有相关位,否则请让我知道。