2

I am using Hibernate Envers to log all changes to the database. I noticed that when I added an entity which had a ManyToOne relation with another entity a revision was made on the one Side entity as well. The foreign key is on the many side of the relation hence when adding an entity on the many side the entity on the one side of the mapping is not updated. But a revision was still being inserted for the entity on the one side of the mapping.

I disabled auditing on the OneToMany mapping using the method suggested here: Whats the difference between @NotAudited and RelationTargetAuditMode.NOT_AUDITED in Hibernate EnVers?

Now this answer suggests that when I retrieve the relation it will always return the current entity. Does that mean I cannot get the information on the One side of the mapping on an old revision?

I am not sure at this point how I am going to query the audited logs but from the table structure it seems I can get the current state of the entities at any point of time.

Suppose I choose not to audit the OneToMany mapping, what will happen for the following table structure?

Audit for OneSideEntity OneSideEntity at revision 1 OneSideEntity at revision 5

Audit for ManySideEntity1 ManySideEntity1 at revision 2 ManySideEntity1 at revision 3 ManySideEntity1 at revision 4

Audit for ManySideEntity2 ManySideEntity2 at revision 6 ManySideEntity2 at revision 7 ManySideEntity2 at revision 8 ManySideEntity2 at revision 9

Both ManySideEntity objects point to OneSideEntity.

What entities will I get when I query the one to many mapping at revision 5 - [ManySideEntity1] or [ManySideEntity1, ManySideEntity2].

The above question suggests I will get the latter but at revision 5 I would prefer the first one. Am I missing something here?

Also for a given timestamp I think I can find all revisions less than that timestamp. This ensures I have accurate table information in the AUDITING log. If the answer is [ManySideEntity1, ManySideEntity2] why is there a loss of information?

Community
  • 1
  • 1
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191

1 Answers1

3

Maybe you'd want to set org.hibernate.envers.revision_on_collection_change to false? (see http://docs.jboss.org/hibernate/core/4.1/devguide/en-US/html/ch15.html#d5e3937)

Whether changing one side of the relation in a bi-directional one-to-many relation changes both sides or not is debatable and depends on the use-case. In the DB, there are no changes, but on the other hand the Java object changes (there's a new element in the collection).

adamw
  • 8,038
  • 4
  • 28
  • 32