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

2024-01-04

将 hibernate 升级到 from 版本后4.3.7.最终 to 5.3.18.决赛我收到以下错误

@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);

    }
    .......
    ...

   }

时间实体 class

@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;
    }

}

时间实体界面

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]

我尝试解决这个问题

add @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]

这个问题与链接线程中提出的问题非常相似。错误映射通过接口公开的嵌入式类 https://stackoverflow.com/questions/1071293/hibernate-and-jpa-error-mapping-embedded-class-exposed-through-an-interface

在 @target 注解中写入要嵌入的实体类名称

   @Embedded
   @Target(TemporalEntity.class)
   private ITemporalEntity temporal;
   
   

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hibernate - 子类必须在其母类之后绑定 的相关文章

随机推荐