【问题标题】:Insert data in two mapped tables in springspring 在两个映射表中插入数据
【发布时间】:2011-12-30 12:41:20
【问题描述】:

我有 2 个表用户和用户配置文件相互映射。

在 User.java 中,userprofile 的 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”,控制器中的提交方法为:

 @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");

}

当我调用用户类的插入方法时,它会插入用户以及用户配置文件表中,但用户配置文件表中的用户 ID 没有更新。

请帮忙。

【问题讨论】:

    标签: hibernate spring spring-mvc hibernate-mapping


    【解决方案1】:

    您应该将用户配置文件添加到用户

    user.setUserProfile(userProfileBean);
    

    还将用户对象添加到 userProfileBean

    userProfileBean.setUser(user);
    

    【讨论】:

    • 谢谢,它现在可以工作了,我错过了 userProfileBean.setUser(user);
    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 2012-06-20
    • 2022-11-11
    • 1970-01-01
    • 2022-01-28
    • 2018-10-09
    • 2016-05-13
    • 2012-10-28
    相关资源
    最近更新 更多