My problem is very similar to this one : Injecting fields via Spring into entities loaded by Hibernate
The difference is that , I am using JPA2 entities , not hibernate . While the underlayer is still hibernate (3.5.5).
My spring version is 3.0.4.
What’s the corresponding eventListeners in JPA’s world ?
Sample code from the original post :
class Student {
int id; //loaded from DB
String name; //loaded from DB
int injectedProperty; //Inject via Spring
transient Service serviceImpl; //Inject via Spring
}
I know there may be aspectJ’s solutions , but I’d rather like a pure-java solution.
Thanks.
JPA has Entity Listeners (and Callback Methods). From the JPA 2.0 specification:
Here is an example (from the spec):
In your case, you’ll probably want
PostLoadcallback methods.See also the Chapter 6. Entity listeners and Callback methods in the Hibernate Entity Manager documentation.
But to put it simply, what you want to do is not that simple with JPA and using AspectJ class weaving with the
@Configurableannotation might be the best option.References