Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting a SessionScoped Stateful bean in EntityListener

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?

like image 477
Tasos P. Avatar asked Oct 08 '22 07:10

Tasos P.


1 Answers

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.

like image 131
Steve K Avatar answered Oct 12 '22 11:10

Steve K