1

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
Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
sanosdole
  • 2,469
  • 16
  • 18

1 Answers1

0

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()
Roger
  • 1,944
  • 1
  • 11
  • 17
  • The Query API is quite limited and it creates a SELECT N+1 Problem when using a complex NHibernate query to fetch the entities and then using an audit-query for each result is not ideal... – sanosdole Mar 24 '15 at 09:51
  • Ok. If you find a better/faster way, feel free to create a pull request to envers repo. About the select n+1 problem... It might be caused by this bug, https://nhibernate.jira.com/browse/NH-2907. Please vote for that one. – Roger Mar 24 '15 at 10:33
  • I thought something along the line of using the audit tables in normal NHibernate queries, as i believe that envers uses NHibernate for accessing those tables. So they must be in the NHibernate-Configuration and by this they should be usable in queries for joining... – sanosdole Mar 24 '15 at 12:39