Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:50:26+00:00 2026-06-07T05:50:26+00:00

I’m trying to implement some sort of auditing in a Java EE JPA (2.0)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:50:28+00:00Added an answer on June 7, 2026 at 5:50 am

    I eventually found a workaround, which allows me to get a reference of the @Stateful bean:

    I created a @Named @Singleton @Startup bean SessionController which holds a local HashMap<String, UserSession> sessionMap with the references of my @Stateful beans:

    @Named
    @Singleton
    @Startup
    @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
    public class SessionController {
    
    private HashMap<String, UserSession> sessionMap;
    
    @PostConstruct
    void init() {
        sessionMap = new HashMap<String, UserSession>();
    }
    
    @PreDestroy
    void terminate() {
        for (UserSession us : sessionMap.values()) {
            us.logoutCleanUp(); //This is annotated as @Remove
        }
        sessionMap.clear();
    }
    
    public void addSession(String sessionId, UserSession us) {
        sessionMap.put(sessionId, us);
        System.out.println("New Session added: " + sessionId);
    }
    
    public UserSession getCurrentUserSession() {
        FacesContext context = FacesContext.getCurrentInstance();
        String sessionId = ((HttpSession) context.getExternalContext().getSession(false)).getId();
        return sessionMap.get(sessionId);
    }
    

    }

    I add the references from within each bean’s @PostConstruct method:

    public class UserSession implements Serializable {
    @Inject SessionController sc;
    ...
        @PostConstruct
        void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        String sessionId = ((HttpSession) context.getExternalContext().getSession(true)).getId();
        sc.addSession(sessionId, this);
    }
    

    Notice the .getSession(true) which is required since the Session might not be created yet. Also notice that this is safely passed since the @PostConstruct is not the constructor…

    After all these, I can get the reference in my EntityListener (and any other place) like this:

    SessionController sc = (SessionController) new InitialContext().lookup("java:module/SessionController");
        UserSession us = sc.getCurrentUserSession();
    

    or like this in CDI beans

    @Inject SessionController sc;
    

    The only drawback I see is that this approach works well only for web applications (where FacesContext context = FacesContext.getCurrentInstance() is meaningful). Some of my beans (and finally my EntityListeners) are also exposed via @javax.jws.WebService as @Stateless beans. In this context (actually: absence of), my Singleton wouldn’t work (haven’t tested yet) since there is no sessionId of any kind (no session at all to be exact). I will have to use a workaround for this, possibly using SessionContext of the bean or inventing a usable sessionId of some sort. I will post back if I create something usable…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.