I'm trying to implement some sort of auditing in a Java EE JPA (2.0) application on GlassFish 3.
I have added a @EntityListeners
annotation on my @MappedSuperclass
entity, the listener has the @PrePersist
and @PreUpdate
annotation on its methods which are invoked happily at runtime.
In these methods, I'm trying to use (@Inject
) a @Named
, @Stateful
, @SessionScoped
bean (UserSession
) in order to get current user's id. The listener class has no annotations at all.
The problem is that I can't get the UserSession
bean injected; I always end up with a null
value. To this time, I tried the plain @Inject UserSession us;
which always injects a null value.I also tried UserSession us = (UserSession) ctx.lookup("java:global/application/module/UserSession");
which always returns a new object (I verified the constructor call, plus the object is empty).
I'm pretty sure I have missed something very important regarding CDI but I can't figure out what. Could someone please point me to the right direction?
EntityListners do not support CDI, at least in JPA 2.0. It's apparently on the list of things new in JPA 2.1
I was also surprised when I ran across this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With