Joining NHibernate.Envers revision information to an audited entity

1k Views Asked by At

Is there an easy way to join the latest revision information for an entity when querying an entity?

Another way would be mapping the properties (using Fluent NHibernate) to the entity, e.g. a entity.LatestRevisionDateTime property.

This is required, as we have some screens that display the latest modification date time and the user by which the entity got modified. We also require at least one query that returns entities that have been modified since a specific date. This would be a lot easier and better performance wise, if we do not need to join in memory (Less DB roundtrips, less memory).

We`re using:

  • NHibernate 4.0.3.400
  • FluentNHibernate 2.0.1.0
  • Hibernate.Envers 2.0.0
1

There are 1 best solutions below

3
On

To get entities and revision info in one statement you can use

session.Auditer().CreateQuery()
   .ForHistoryOf<YourEntityType, YourRevisionType>()
   .Add([some filters])
   .Results()

To get the latest/current entity together with revision object, I think this works (AFK so can't verify)

session.Auditer().CreateQuery()
   .ForHistoryOf<YourEntityType, YourRevisionType>()
   .Add(AuditEntity.RevisionNumber().Maximize().ComputeAggregationInInstanceContext())
   .Results()