I have a class with @Audit annotation as below
@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
@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..
I'll add what I mentioned in HipChat here for posterity sake.
What you are attemping to do is to use the AuditReader#find
method by specifying a specific value of your composite-id class. 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:
// 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.
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.
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.