我有一个实体BlocRecord有一个复合代码BlocRecordId,并且在它的复合代码中有一个@Embedded(关系代码ManyToOne)指向另一个entiy Record并希望审计实体BlocRecord。
实体BlocRecord
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "blocRecord")
@Access(value = AccessType.FIELD)
@Audited
public class BlocRecord {
@EmbeddedId
private BlocRecordId blocRecordId = new BlocRecordId();
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumns({
@JoinColumn(name = "record_identifier_", referencedColumnName = "identifier_", unique = false, nullable = false),
@JoinColumn(name = "record_recordType_", referencedColumnName = "recordType_", unique = false, nullable = false)})
@MapsId("record")
private Record record;
...
}
id类BlocRecordID
@Embeddable
public class BlocRecordId implements Serializable {
@Embedded
private RecordId record;
@Column(name = "source_")
String source ;
@Column(name = "messageType_")
String messageType ;
实体记录
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "records")
@Access(value = AccessType.FIELD)
@Audited
public class Record {
@EmbeddedId
private RecordId recordId = new RecordId();
@OneToMany(targetEntity = BlocRecord.class, fetch = FetchType.LAZY, mappedBy = "record")
private Set<BlocRecord> blocRecord = new java.util.HashSet<>();
...
}
实体Record的idClass
@Embeddable
public class RecordId implements Serializable{
@Column(name = "identifier_")
String identifier ;
@Column(name = "recordType_")
String recordType ;
}
当尝试在可嵌入的BlocRecordId中生成字段记录的元数据时,Hibernate-envers失败,抛出流动的异常
org.hibernate.MappingException: Type not supported: org.hibernate.type.ComponentType
at org.hibernate.envers.configuration.internal.metadata.IdMetadataGenerator.addIdProperties(IdMetadataGenerator.java:121)
at org.hibernate.envers.configuration.internal.metadata.IdMetadataGenerator.addId(IdMetadataGenerator.java:230)
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:642)
at org.hibernate.envers.configuration.internal.EntitiesConfigurator.configure(EntitiesConfigurator.java:95)
at org.hibernate.envers.boot.internal.EnversServiceImpl.doInitialize(EnversServiceImpl.java:154)
at org.hibernate.envers.boot.internal.EnversServiceImpl.initialize(EnversServiceImpl.java:118)
at org.hibernate.envers.boot.internal.AdditionalJaxbMappingProducerImpl.produceAdditionalMappings(AdditionalJaxbMappingProducerImpl.java:99)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:288)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:417)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:86)
at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:179)
你知道如何解决这个问题吗?
谢谢
目前,当我们映射标识符列时,Envers不支持将嵌入嵌套在嵌入内部的想法,如您的示例所示。 Envers目前支持的唯一有效映射是嵌入的属性是@ManyToOne
还是@Basic
类型。
你可以解决这个问题,但它涉及更明确,而不是使用RecordId
。我的意思是重写BlocRecordId
如下:
@Embeddable
public class BlocRecordId implements Serializable {
@Column(name = "identifier_")
String identifier;
@Column(name = "recordType_")
String recordType;
@Column(name = "source_")
String source;
@Column(name = "messageType_")
String messageType;
@Transient
private RecordId recordId;
/** Helper method to assign the values from an existing RecordId */
public void setRecordId(RecordId recordId) {
this.identifier = recordId.getIdentifier();
this.recordType = recordId.getRecordType();
}
/** Helper method to get the RecordId, caching it to avoid multiple allocations */
public RecordId getRecordId() {
if ( recordId == null ) {
this.recordId = new RecordId( identifier, recordType );
}
return this.recordId;
}
}
我同意这不太理想,但至少可以解决目前的代码限制。我已经添加并添加了HHH-13361作为一个开放的问题来支持这一点。如果您愿意,我们欢迎您的贡献,或者我将努力为Envers 6.0提供支持。