简体   繁体   English

Hibernate Envers无法为可嵌入表“定位属性的获取方法”

[英]Hibernate Envers is not able to “locate getter method for property” for Embeddable table

I have a class with @Audit annotation as below 我有一个带有@Audit注释的类,如下所示

@Entity
@Audited(withModifiedFlag=true)
@Table(name = "PERIODICITY")
public class Periodicity implements java.io.Serializable {

private PeriodicityId id;
private String frequency;

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "instrumentId", column = @Column(name = "INSTRUMENT_ID", nullable = false, precision = 22, scale = 0)),
        @AttributeOverride(name = "legType", column = @Column(name = "LEG_TYPE", nullable = false, precision = 38, scale = 0))})
public PeriodicityId getId() {
    return this.id;
}
public void setId(PeriodicityId id) {
    this.id = id;
}
@Column(name = "FREQUENCY", nullable = false, length = 20)
public String getFrequency() {
    return this.frequency;
}
}

And the Embedded class is as follows 而Embedded类如下

@Embeddable
public class PeriodicityId implements java.io.Serializable {

private Long instrumentId;
private Long legType;
@Column(name = "INSTRUMENT_ID", nullable = false, precision = 22, scale = 0)
public Long getInstrumentId() {
    return this.instrumentId;
}

public void setInstrumentId(Long instrumentId) {
    this.instrumentId = instrumentId;
}

@Column(name = "LEG_TYPE", nullable = false, precision = 38, scale = 0)
public Long getLegType() {
    return this.legType;
}
}

And through audit reader I'm trying to find Audit at particular revision as follows 通过审核阅读器,我尝试按以下特定版本查找审核

Session session = HibernateUtil.currentSession();
AuditReader reader = AuditReaderFactory.get(session);

Periodicity periodicity = reader.find( Periodicity.class, instrumentId, revision_Id);

But its giving exception like 但它给人例外

org.hibernate.PropertyNotFoundException: Could not locate getter method for property [java.lang.Long#instrumentId]
at org.hibernate.internal.util.ReflectHelper.findGetterMethod(ReflectHelper.java:408)
at org.hibernate.property.access.internal.PropertyAccessBasicImpl.<init>(PropertyAccessBasicImpl.java:41)
at org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl.buildPropertyAccess(PropertyAccessStrategyBasicImpl.java:27)
at org.hibernate.envers.internal.tools.ReflectionTools.getGetter(ReflectionTools.java:53)

Please Help how to access property of Embeddable class.. 请帮助如何访问Embeddable类的属性。

I'll add what I mentioned in HipChat here for posterity sake. 为了后代,我将在此处添加我在HipChat中提到的内容。

What you are attemping to do is to use the AuditReader#find method by specifying a specific value of your composite-id class. 您试图通过指定Composite-id类的特定值来使用AuditReader#find方法。 The method signature that you're using expects the actual embeddable class and not a specific attribute type the embeddable contains. 您使用的方法签名应使用实际的可嵌入类,而不是可嵌入包含的特定属性类型。

The proper usage of AuditReader#find would be like: AuditReader#find的正确用法如下:

// define your embeddable class attributes
final PeriodicityId id = new PeriodicityId();
id.setInstrumentId( instrumentId );

// lookup revision 1
final Number revisionId = 1; 

// query
final AuditReader auditReader = AuditReaderFactory.get( session );
auditReader.find( Periodicity.class, id, revisionId );

Whlie this avoids the exception you encountered, this won't give you the expected results because the embeddable predicates will assume you're interested in Perodicity instances where the legType attribute is null which is not your goal. Whlie这避免您遇到的异常,这不会给你所期望的结果,因为嵌入谓词将假定你有兴趣Perodicity情况,其中legType属性为null,这是不是你的目标。

The only way you can accomplish the goal of your task then is to use Envers adhoc query features where you specify the precise predicates to target the results you're interested in. 然后,完成任务目标的唯一方法是使用Envers addhoc查询功能,在其中您可以指定精确谓词来定位感兴趣的结果。

final AuditReader auditReader = AuditReaderFactory.get( session );
List results = auditReader.createQuery()
  .forRevisionsOfEntity( Periodicity.class, true, false )
  // add the revision number predicate
  .add( AuditEntity.revisionNumber().eq( revisionId ) )
  // add the instrument predicate
  .add( AuditEntity.property( "id.instrumentId" ).eq( instrumentId ) )
  .getResultList();

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM