Hibernate-子类必须在其父类之后绑定

问题描述

将休眠从版本 4.3.7.Final 升级 5.3.18.Final 后,我得到了以下错误

@Entity
@Audited
@AuditPermission(Permission.VIEW_INDIVIDUAL)
public class Individual implements ITemporalEntity {

    @Id
    @Column(name = "Individual_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "Individual_generator")
    @SequenceGenerator(name = "Individual_generator",initialValue = 1,allocationSize = 1,sequenceName = "Individual_id_seq")
    private Long id;
    @Embedded
    private TemporalEntity temporal = new TemporalEntity();
    @Override
    public DateTime getCreateDate() {
     return temporal.getCreateDate();
    }

    @Override
    public void setCreateDate(DateTime createDate) {
     temporal.setCreateDate(createDate);

    }
    .......
    ...

   }

TemporalEntity

@Embeddable
public class TemporalEntity {

@Column(updatable = false)
private DateTime createDate;

@Column
private DateTime lastModifiedDate;

@ManyToOne
@JoinColumn(name = "created_by_id",updatable = false)
private AdminUser createdBy;

@ManyToOne
@JoinColumn(name = "last_modified_by_id")
private AdminUser lastModifiedBy;

@Column(nullable = false,columnDeFinition = "boolean not null default false")
private boolean deleted = false;

public DateTime getCreateDate() {
    return createDate;
}

public void setCreateDate(DateTime createDate) {
    if (createDate == null) {
        //ignore attempts to clear this field
        return;
        //throw new IllegalStateException("Null create date not allowed");
    }
    this.createDate = createDate;
}

public DateTime getLastModifiedDate() {
    return lastModifiedDate;
}

public void setLastModifiedDate(DateTime lastModifiedDate) {
    if (lastModifiedDate == null) {
        //ignore attempts to clear this field
        return;
        //throw new IllegalStateException("Null last modified date not allowed");
    }
    this.lastModifiedDate = lastModifiedDate;
}

public AdminUser getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(AdminUser createdBy) {
    if (createdBy == null) {
        //ignore attempts to clear this field
        return;
        //throw new IllegalStateException("Null created by not allowed");
    }
    this.createdBy = createdBy;
}

public AdminUser getLastModifiedBy() {
    return lastModifiedBy;
}

public void setLastModifiedBy(AdminUser lastModifiedBy) {
    if (lastModifiedBy == null) {
        //ignore attempts to clear this field
        return;
        //throw new IllegalStateException("Null lastModifiedBy not allowed");
    }
        this.lastModifiedBy = lastModifiedBy;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

}

ITemporalEntity 接口

public interface ITemporalEntity {

    public DateTime getCreateDate();

    public void setCreateDate(DateTime createDate);

    public DateTime getLastModifiedDate();

    public void setLastModifiedDate(DateTime lastModifiedDate);

    public AdminUser getCreatedBy();

    public void setCreatedBy(AdminUser createdBy);

    public AdminUser getLastModifiedBy();

    public void setLastModifiedBy(AdminUser lastModifiedBy);

    public boolean isDeleted();

    public void setDeleted(boolean deleted);

}

错误堆栈

an assertion failure occurred (this may indicate a bug in Hibernate,but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class: com.berwick.dal.TemporalEntity
23:11:29,486 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 87) MSC000001: Failed to start service jboss.persistenceunit."bds-core-1.0-SNAPSHOT.war#com.berwick.dal": org.jboss.msc.service.StartException in service jboss.persistenceunit."bds-core-1.0-SNAPSHOT.war#com.berwick.dal": org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class: com.berwick.dal.TemporalEntity
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:198) [wildfly-jpa-21.0.0.Final.jar:21.0.0.Final]

我尝试解决此问题

@MappedSuperclass添加到TemporalEntity类 使得该错误消失了,但我遇到了更多错误

Duplicate generator name Individual_generator you will likely want to set the property hibernate.jpa.compliance.global_id_generators to false 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:198) [wildfly-jpa-21.0.0.Final.jar:21.0.0.Final]

解决方法

问题与链接线程中的问题非常相似。 Error Mapping Embedded class exposed through an interface

输入要嵌入@target批注中的实体类名称

const Level_2_8 = () => {
const t = `./wait.png`
        switch (level) {
            case 61: return ( < Image source={require(`${t}`)} style={{ width: 50,height: 60 }} />)
                break;
            case 62: return (< Image source={require(`${t}`)} style={{ width: 50,height: 60 }} />)
                break;
            default: (< Image source={require(`${t}`)} style={{ width: 50,height: 60 }} />)
        }
    }

您不需要通过创建嵌入的新对象来紧密耦合TemporalEntity

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...