Reputation: 3224
I have written my custom RevisionEntity class to store additional data (for example username), like below:
@Entity
@RevisionEntity(AuditListener.class)
@Table(name = "REVINFO", schema = "history")
@AttributeOverrides({
@AttributeOverride(name = "timestamp", column = @Column(name = "REVTSTMP")),
@AttributeOverride(name = "id", column = @Column(name = "REV")) })
public class AuditEntity extends DefaultRevisionEntity {
private static final long serialVersionUID = -6578236495291540666L;
@Column(name = "USER_ID", nullable = false)
private Long userId;
@Column(name = "USER_NAME")
private String username;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
I can see that all rows in database are correctly stored, REVINFO table contains also username.
I would like to query database to get detailed information from my custom RevisionEntity, like username. How can I do it? Is there any supported API to get it?
Upvotes: 0
Views: 2332
Reputation: 21153
Lets assume you know the identifier of the entity you're interested in the revision entity metadata for, you can easily query that information using the following approach:
final AuditReader auditReader = AuditReaderFactory.get( session );
List<?> results = auditReader.createQuery()
.forRevisionsOfEntity( YourEntityClass.class, false, false )
.add( AuditEntity.id().eq( yourEntityClassId ) )
.getResultList();
The returned results will contain an Object array, e.g. Object[]
where results[1]
will hold the revision entity instance which contains the pertinent information your wanting.
For more details, you can see the java documentation comments here
If you only have the revision number, you can access just the revision entity instance directly by:
// I use YourAuditEntity here because AuditEntity is actually an Envers class
YourAuditEntity auditEntity = auditReader
.findRevision( YourAuditEntity.class, revisionId );
For more details on the AuditReader interface, you can see the java documentation here
Upvotes: 3