java Hibernate 尝试从 null 一对一属性分配 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39432871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:22:49  来源:igfitidea点击:

Hibernate attempted to assign id from null one-to-one property

javahibernate

提问by Feeco

I have two entities:

我有两个实体:

@Entity
@Table
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
    private UserDetails userDetails;

}

@Entity
@Table(name="user_details")
public class UserDetails {

    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @Parameter(name = "property", value = "user"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(unique = true, nullable = false)
    private Integer id;

    @OneToOne
    @PrimaryKeyJoinColumn
    private User user;

    public UserDetails(User user) {
        this.user = user;
        user.setUserDetails(this);
    }

}

It works if I create a user with a userDetails. But then it creates a UserDetails row and I don't want it. I have to fetch a User from database and add a UserDetails later:

如果我使用 userDetails 创建用户,它会起作用。但随后它创建了一个 UserDetails 行,我不想要它。我必须从数据库中获取一个 User 并稍后添加一个 UserDetails:

userRepository.findOneById(id).map(user -> {
    UserDetails userDetails = user.getUserDetails();
    if (userDetails == null)
        userDetails = new UserDetails(user); 
     userDetails.setEmail(email);
     userRepository.save(user); //error here
});

Error

错误

org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.app.domain.UserDetails.user]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.app.domain.UserDetails.user]

org.springframework.orm.jpa.JpaSystemException:试图从空一对一属性 [com.app.domain.UserDetails.user] 中分配 id;嵌套异常是 org.hibernate.id.IdentifierGenerationException:试图从 null 一对一属性 [com.app.domain.UserDetails.user] 分配 id

回答by Trevor

The UserDetailsobject in this case is the owning side of the relationship between a Userand the UserDetails

UserDetails在这种情况下,对象是 aUser和 the之间关系的拥有方UserDetails

Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship.

受管实体之间的双向关系将基于关系拥有方持有的引用而持久化。

Therefore, when you say userRepository.save(user), you're actually trying to save from the child side of the relationship.

因此,当您说 时userRepository.save(user),您实际上是在尝试从关系的孩子方面进行挽救。

You need to create a UserDetailsRepositoryand invoke the save from that new object.

您需要创建一个UserDetailsRepository并从该新对象调用保存。

i.e. here's what the code should look like:

即这里的代码应该是这样的:

userRepository.findOneById(id).map(user -> {
    UserDetails userDetails = user.getUserDetails();
    if (userDetails == null)
        userDetails = new UserDetails(user); 
     userDetails.setEmail(email);
     userDetailsRepository.save(userDetails);
});