如何解决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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。