Hibernate 在spring中的两个映射表中插入数据
我有两个表user和userprofile相互映射 在User.java中,userprofile的getter方法是:Hibernate 在spring中的两个映射表中插入数据,hibernate,spring,spring-mvc,hibernate-mapping,Hibernate,Spring,Spring Mvc,Hibernate Mapping,我有两个表user和userprofile相互映射 在User.java中,userprofile的getter方法是: @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") public Userprofile getUserprofiles() { return this.userprofiles; } 在Userprofile.java中,用户的getter方法是:
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Userprofile getUserprofiles() {
return this.userprofiles;
}
在Userprofile.java中,用户的getter方法是:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserID")
public User getUser() {
return this.user;
}
在User.jsp中,commandName是“userprofile”,controller中的submit方法是:
@RequestMapping("/insertUser.do")
public ModelAndView showLoginForm( @ModelAttribute("userprofile") Userprofile userProfileBean,BindingResult result, HttpServletRequest request, HttpServletResponse response )throws Exception
{
java.util.Date utilDate=new java.util.Date();
java.sql.Timestamp timest=new Timestamp(utilDate.getTime());
userProfileBean.setEntryDate(timest);
User user = new User();
user.setUserProfile(userProfileBean);
this.userServ.createUser(user);
return new ModelAndView(new RedirectView("usersMapping.do"), "Test", "Test");
}
当我调用user类的insert方法时,它将插入到user和userprofile表中,但在userprofile表中userid没有更新
请提供帮助。您应该将用户配置文件添加到用户配置文件中
user.setUserProfile(userProfileBean);
还将用户对象添加到userProfileBean
userProfileBean.setUser(user);
谢谢,它现在正在工作,我缺少userProfileBean.setUser(user);