Hibernate - Subclass has to be binded after it's mother class

自闭症网瘾萝莉.ら 提交于 2021-01-28 07:50:24

问题


after upgrading hibernate to from version 4.3.7.Final to 5.3.18.Final I got the error below

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

}

ITemporalEntity interface

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

}

the error stack

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]

My tries to fix the issue

add @MappedSuperclass to TemporalEntity class that makes this error gone but I got more errors

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]

回答1:


The question is quite similar asked in linked thread. Error Mapping Embedded class exposed through an interface

write your entity class name that you want to embed in the @target annotation

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

you do not need to tightly couple the TemporalEntity by creating new object embedded do it



来源:https://stackoverflow.com/questions/64688273/hibernate-subclass-has-to-be-binded-after-its-mother-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!