【问题标题】:JPA: One to One relationship with Entity of exact typeJPA:与确切类型的实体的一对一关系
【发布时间】:2015-09-14 12:44:03
【问题描述】:

假设你有如下数据库表:

CREATE TABLE IF NOT EXISTS USER (
    USER_NAME               VARCHAR(20) NOT NULL,
    FIRST_NAME              VARCHAR(50) NOT NULL,
    SECOND_NAME             VARCHAR(50) NOT NULL DEFAULT '',
    SURNAME                 VARCHAR(50) NOT NULL,
    BIRTH_DATE              DATE NOT NULL,
    BIRTH_GENDER            ENUM ('M', 'F') NOT NULL,
    CREATION_TIMESTAMP      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CREATED_BY              VARCHAR(20) NOT NULL,
    LAST_UPDATE_TIMESTAMP   TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    LAST_UPDATED_BY         VARCHAR(20),
    DELETION_TIMESTAMP      TIMESTAMP NULL,
    DELETED_BY              VARCHAR(20),

    PRIMARY KEY (USER_NAME),
    FOREIGN KEY (CREATED_BY) REFERENCES USER(USER_NAME),
    FOREIGN KEY (LAST_UPDATED_BY) REFERENCES USER(USER_NAME),
    FOREIGN KEY (DELETED_BY) REFERENCES USER(USER_NAME)
) CHARACTER SET utf8;

我有如下JPA实体:

/**
 * @author Buhake Sindi
 *
 */
@Entity
@Table(name="USER")
@Access(AccessType.FIELD)
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1600376007707987934L;

    @Id
    @Column(name="USER_NAME", nullable=false)
    private String id;

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

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

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

    @Temporal(TemporalType.DATE)
    @Column(name="BIRTH_DATE", nullable=false)
    private Date birthDate;

    @Column(name="BIRTH_GENDER", columnDefinition="ENUM('M', 'F')", nullable=false)
    @Enumerated(EnumType.STRING)
    private Gender birthGender;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CREATION_TIMESTAMP", insertable=false, updatable=false, nullable=false)
    private Date creationTimestamp;

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="CREATED_BY", insertable=true, updatable=false, nullable=false)
    private User createdBy;

    /* (non-Javadoc)
     * @see za.co.sindi.entity.IDBasedEntity#getId()
     */
    public String getId() {
        // TODO Auto-generated method stub
        return id;
    }

    /* (non-Javadoc)
     * @see za.co.sindi.entity.IDBasedEntity#setId(java.io.Serializable)
     */
    public void setId(String id) {
        // TODO Auto-generated method stub
        this.id = id;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the secondName
     */
    public String getSecondName() {
        return secondName;
    }

    /**
     * @param secondName the secondName to set
     */
    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the birthDate
     */
    public Date getBirthDate() {
        return birthDate;
    }

    /**
     * @param birthDate the birthDate to set
     */
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * @return the birthGender
     */
    public Gender getBirthGender() {
        return birthGender;
    }

    /**
     * @param birthGender the birthGender to set
     */
    public void setBirthGender(Gender birthGender) {
        this.birthGender = birthGender;
    }


    /* (non-Javadoc)
     * @see za.co.sindi.entity.AbstractIdentifiableAuditableEntity#getCreationTimestamp()
 */
    @Override
    public Date getCreationTimestamp() {
    // TODO Auto-generated method stub
    return creationTimestamp;
}

/* (non-Javadoc)
 * @see za.co.sindi.entity.AbstractIdentifiableAuditableEntity#setCreationTimestamp(java.util.Date)
 */
@Override
public void setCreationTimestamp(Date creationTimestamp) {
    // TODO Auto-generated method stub
    this.creationTimestamp = creationTimestamp;
}

/* (non-Javadoc)
 * @see za.co.sindi.entity.AbstractIdentifiableAuditableEntity#getCreatedBy()
 */
@Override
public User getCreatedBy() {
    // TODO Auto-generated method stub
    return createdBy;
}

/* (non-Javadoc)
 * @see za.co.sindi.entity.AbstractIdentifiableAuditableEntity#setCreatedBy(java.lang.Object)
 */
    @Override
    public void setCreatedBy(User createdBy) {
        // TODO Auto-generated method stub
        this.createdBy = createdBy;
    }
}

问题在于将属性createdBy 映射到CREATED_BY 列时。将其部署到 RedHat Wildfly 8.2.0 Final 时,它会陷入无限循环(StackOverflow 错误)。如何正确映射它,以便在 JPA 不发疯的情况下建立一对一关系?


我不知道为什么这是相关的,但我的persistence.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="myapp-persistence-unit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider><!-- used since Hibernate 4.3.x instead of org.hibernate.ejb.HibernatePersistence -->
        <jta-data-source>java:/jdbc/MyAppXADS</jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode><!-- This is the default anyway. -->
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate" /><!-- DBA's are paid to create DB tables so never do update | create | create-drop. -->
            <property name="hibernate.cache.use_second_level_cache" value="true" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        </properties>
    </persistence-unit> 
</persistence>

【问题讨论】:

  • 你能添加你的persistence.xml
  • 我使用的是 JPA 2.1 Persistence 框架,为什么需要我的persistence.xml
  • 我使用了你的sql和实体定义,效果很好,我想看看配置,只是为了在我的环境中模拟
  • 查看我更新的帖子。
  • 没发现什么问题,投了赞成票,希望其他开发者可以帮忙

标签: java jpa jpa-2.0


【解决方案1】:

映射看起来不错,所以我认为它一定是导致问题的实际数据库状态。

您将CREATED_BY 声明为NOT NULL;那么如何在空的USER 表中插入第一个用户呢?解决这个问题的唯一方法是指出第一个用户实际上是自己创建的。

如果是这种情况(或者您出于任何其他原因有自引用用户,例如,如果这是您保存注册自己并且不是由其他用户创建的用户的方式),那么有很多无限递归的可能性。例如,检查您的toStringequalshashCode(我在您发布的User 课程中看不到它们,也许您为了简洁而省略了它们)。

我希望 Hibernate 能够处理这种情况,但它可能会尝试在同一个 User 实例中重复解析 createdBy 关联。

无论是什么原因,错误的堆栈跟踪都应该提示无限递归的来源。

此外,您可能希望为 org.hibernate 包启用 TRACE 日志级别以查看究竟发生了什么。

【讨论】:

  • The only way to solve it is to indicate that the first user actually created herself.。答对了!只有这样,你才能创造自己。我将发布发生的实际问题以及我如何解决它。
【解决方案2】:

这个问题的实际问题是 User 实体是 @MappedSuperclass AbstractSoftDeleteAuditableEntity 的子类(这是通用的)。在MappedSuperclass 实体中,createdBylastUpdatedBydeletedBy 属性被分配给有界类型User。在部署应用程序时,Hibernate 进入一个循环循环,试图验证User 实体并导致StackOverflowError。我将User 实体设置为一个简单的实体,现在所有实体都已部署并验证无误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多