spring - 使用 Spring 和 JSP 的 Hibernate OneToOne

标签 spring hibernate jsp

我有一个问题要问:

我必须将表 user 和 user_login 通过 user.id -> user_login.user_id 加入 OneToOne。

问题是当我执行 .updateObject(user) 时,我执行了 2 个查询:

Hibernate: insert into User (created, modified, email, first_name, last_name) values (?, ?, ?, ?, ?) Hibernate: insert into user_login (created, modified, password, user_id) values (?, ?, ?, ?) [2012-08-15 12:15:04,192] [ERROR] [http-bio-8080-exec-1] SqlExceptionHelper [144]: Column 'user_id' cannot be null

看起来两个对象之间没有引用。如果进入实体用户,方法 setUserLogin 我添加行 userLogin.setUser(this);它有效,但老实说我不觉得这种方式优雅。实体配置中有什么我错过的吗 也许这不会自动做到这一点?

谢谢

这是我的实体

@Entity
@NamedQueries({ @NamedQuery(name = "user.list", query = "select u from User u") })
public class User implements java.io.Serializable {

    @Column(name = "first_name", nullable = true)
    private String firstName;   

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="user_id", nullable=false)
    private UserLogin userLogin;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public UserLogin getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(UserLogin userLogin) {
        this.userLogin = userLogin;
        //userLogin.setUser(this); THIS IS THE LINE THAT FIXES IT, BUT I DONT FIND THIS WAY ELEGANT
    }

}


@Entity
@Table(name="user_login")
public class UserLogin implements java.io.Serializable {

    @Column(name = "password", nullable = false)
    private String password;


    @OneToOne(optional = false, fetch = FetchType.LAZY) 
  private User user;


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }   

}

JSP 文件:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
    <form:form action="/test" commandName="user">
        <tr>
            <td>User Name :</td>
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:input path="userLogin.password" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Register"></td>
        </tr>
        </table>
    </form:form>
</body>
</html>

Spring Controller :

@Controller(value = "/")
public class Test { 

  @Autowired
    UserServiceImpl userServiceImpl;

  @RequestMapping(method = RequestMethod.GET, value = "/test")
  public void test(ModelMap model) {

    model.addAttribute("user", new User());

  }

  @RequestMapping(method = RequestMethod.POST, value = "/test")
  public void test(User user) {

    userServiceImpl.update(user);    

  }
} 

最佳答案

像往常一样,双向关系确实有拥有方。关系的拥有方是由mappedBy 引用的属性。在您的情况下,UserLogin 实体中的属性user 是拥有方。

当关系持久化到数据库时,仅咨询拥有方。这意味着,您必须为 user 属性设置值才能保留。为了使内存中的实体图保持一致,应该设置关系的双方。

在 JPA 2.0 规范中,这是用以下文字来说明的:

Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change.

关于spring - 使用 Spring 和 JSP 的 Hibernate OneToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11973116/

相关文章:

java - Multipart transferTo 在使用 createTempFile 时查找错误的文件地址

java - 我应该使用哪种方法在 Spring Controller 中注入(inject) 10 多个服务?

java - 从按钮单击调用的 jsp scriptlet 函数

javascript - 映射到 <bean> 到 knockout .js

java - 查找 spring bean 之间循环依赖的良好实践

java - Spring AOP @Pointcut 未触发

java - hibernate如何在表中间插入记录?

java - Hibernate 返回 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Duplicate entry

hibernate - 将 Hibernate 与 Presto 结合使用

javax.el.PropertyNotFoundException : No public static field named [ShoppingBag] was found on class [Others. 客户]