有没有办法使用hibernate-envers审核在EmbeddedId中具有@Embedded的实体

时间:2019-04-05 07:23:29

标签: java hibernate hibernate-envers

我有一个实体BlocRecord,它的复合代码为BlocRecordId,并且在其复合代码中有一个@Embedded(关系代码ManyToOne)指向另一个完整的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)

您知道如何解决此问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

目前,如我们的示例所示,当我们映射标识符列时,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提供支持。