JPA:与确切类型的实体的一对一关系

时间:2015-06-27 18:39:09

标签: java jpa jpa-2.0

假设您有以下数据库表:

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>

2 个答案:

答案 0 :(得分:2)

映射看起来很好,所以我认为它必须是导致问题的实际数据库状态。

您宣布CREATED_BYNOT NULL;那么如何将第一个用户插入空的USER表中?解决它的唯一方法是指出第一个用户实际创建了自己。

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

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

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

此外,您可能希望为TRACE程序包启用org.hibernate日志级别,以查看具体情况。

答案 1 :(得分:1)

此问题的实际问题是User实体是@MappedSuperclass AbstractSoftDeleteAuditableEntity的子类(通用版)。在MappedSuperclass实体中,createdBylastUpdatedBydeletedBy属性已分配给有界类型User。在部署应用程序时,Hibernate进行了循环循环,尝试验证User实体并生成StackOverflowError。我使User实体成为一个简单的实体,现在所有实体都已部署和验证,没有错误。