OneToOne在具有共享主键的两个表之间

OneToOne在具有共享主键的两个表之间

本文介绍了OneToOne在具有共享主键的两个表之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用JPA / Hibernate设置以下表:

I'm trying to set up the following tables using JPA/Hibernate:

User:

userid - PK
name

Validation:

userid - PK, FK(user)
code

可能有许多用户,每个用户最多可以有一个验证码或无。

There may be many users and every user may have max one validation code or none.

这是我的课程:

public class User
{
    @Id
    @Column(name = "userid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long userId;

    @Column(name = "name", length = 50, unique = true, nullable = false)
    protected String name;

    ...
}

public class Validation
{
    @Id
    @Column(name = "userid")
    protected Long userId;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn(name = "userid", referencedColumnName = "userid")
    protected User user;

    @Column(name = "code", length = 10, unique = true, nullable = false)
    protected String code;

    ...

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

    ...
}

我创建一个用户,然后尝试使用以下代码添加验证代码:

I create a user and then try to add a validation code using the following code:

public void addValidationCode(Long userId)
{
    EntityManager em = createEntityManager();
    EntityTransaction tx = em.getTransaction();

    try
    {
        tx.begin();

        // Fetch the user
        User user = retrieveUserByID(userId);

        Validation validation = new Validation();
        validation.setUser(user);
        em.persist(validation);
        tx.commit();
    }
    ...
}

运行它我得到一个org.hibernate.PersistentObjectException:分离的实体传递给持久:用户

When I try to run it I get a org.hibernate.PersistentObjectException: detached entity passed to persist: User

我也试图在我的验证类中使用下面的代码:

I have also tried to use the following code in my Validation class:

public void setUserId(Long userId)
{
    this.userId = userId;
}

,当我创建验证码时,我只需:

and when I create a validation code I simply do:

Validation validation = new Validation();
validation.setUserId(userId);
em.persist(validation);
tx.commit();

但是,因为User是null,我得到org.hibernate.PropertyValueException:not-null属性引用null或临时值:User.code

But then since User is null I get org.hibernate.PropertyValueException: not-null property references a null or transient value: User.code

感谢有关如何最好地解决这个问题的任何帮助!

Would appreciate any help regarding how to best solve this issue!

推荐答案

如果您使用Hibernate,还可以使用

If you use Hibernate you can also use

public class Validation {

    private Long validationId;
    private User user;

    @Id
    @GeneratedValue(generator="SharedPrimaryKeyGenerator")
    @GenericGenerator(name="SharedPrimaryKeyGenerator",strategy="foreign",parameters =  @Parameter(name="property", value="user"))
    @Column(name = "VALIDATION_ID", unique = true, nullable = false)
    public Long getValidationId(){
        return validationId;
    }

    @OneToOne
    @PrimaryKeyJoinColumn
    public User getUser() {
        return user;
    }

}

Hibernate会确保ID的验证将与用户实体集的ID相同。

Hibernate will make sure that the ID of Validation will be the same as the ID of the User entity set.

这篇关于OneToOne在具有共享主键的两个表之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:43