Maybe @PrePersist, @PreUpdate, @PreRemove could do the trick?
Nope. @PrePersist, @Pre.... won't do as I can't run a JPA Query and even if manipulating entities via relationships, a flush could occur resulting in possible recursion....
You inject(spring, spring/seam integration) or lookup(JNDI) a SessionFactory to your interceptor instance. If you try injection, inject the SessionFactory in a static field so that the separate Interceptor instance created on the persistence unit creation will have a hold to the same ref.
You can then open an independent session to perform your query inside your interceptor code.
You can add the below to activate your interceptor in your persistence.xml. You will have only 1 interceptor instance shared across all sessions to it has to be thread-safe.
<property name="hibernate.ejb.interceptor" value="a.b.c.YourInterceptor" />
To avoid possible recursive calls to your interceptor pass
when used inside the interceptor. EmptyInterceptor will override the default interceptor set in persistence.xml.
All of this ain't very pretty but I believe it's a workable solution.
Hmm. Not too sure. Can't really use a second Session don't think this will fit for me.
Wondering whether a javax.transaction.Synchronization is the way forward....