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

  • SEARCH
  • Home
  • 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 7994989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:16:34+00:00 2026-06-04T14:16:34+00:00

After constructing the bean, I want to retrieve data from the database, using the

  • 0

After constructing the bean, I want to retrieve data from the database, using the EntityManager. It is not possible in the constructor, because the EntityManager is injected after the constructor is called. So I tried to do it in a method annotated with @PostConstruct. According to the API, a PostConstruct Methods gets called after all injection are done. Executing the query works, but it always returns an empty list. If I use the same query in an other method, it returns the correct result. Does anybody know, why it does not work in the PostConstruct method?

@Stateful(mappedName = "price")
@Singleton
@Startup
public class PriceManagementBean implements PriceManagement {

    @PersistenceContext
    private EntityManager em;

    private List<PriceStep> priceSteps =  Collections.synchronizedList(new ArrayList<PriceStep>());


    public PriceManagementBean(){


    }


    @PostConstruct
    public void init(){
        javax.persistence.Query query = em.createQuery("SELECT ps FROM PriceStep ps");
        List<PriceStep> res = query.getResultList();
            .....
       }
}
  • 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-04T14:16:35+00:00Added an answer on June 4, 2026 at 2:16 pm

    Does anybody know, why it does not work in the PostConstruct method?

    Reason 1
    You cannot create a bean that is at the same time @Stateful and @Singleton(Well you can but it will make no sense since Singletons are also Stateful), that is one of the reasons you are having trouble. There is no exceptions, but there is a conflict in there, you need to fix that first.

    Just remember:

    • A Singleton bean is a bean that mantains its state. There is only one instance of a Singleton in an application and it is shared among all the users of the app. Also since it is a shared(maybe better say concurrent) bean, there is need to implement some kind of locking mechanism using the @Lock annotation.

    • A Stateful bean is a bean that mantains each state after a transaction. When working with
      Stateful beans each user gets a copy of the bean which will last as long as the session – lasts or until a method annotated with @Remove is called

    Reason 2
    Even if it works, you will be unable to access the results, because you are storing them in an object called res which is only accessible from inside the method init(). I suppose you would like to assign that returned value to the variable priceSteps.

    Anyway there are many things wrong in your code, for not saying everything. I don’t know what are your system requirements, but here i would give you a simple solution that will allow you to access the database:

    I suppose you are trying to in some way return the data in the life cycle of the bean because you want to avoid sending queries again and again if the bean is @Stateful.
    The thing is, you don’t have to do that, you can still make your bean @Stateless and avoid stressing your database with many queries.
    What you need to do is create a @NamedQuery.

    So annotate your entity PriceStep with @NamedQuery and there enter the query string you wrote. In this link you will find information about how to use @NamedQueries:
    http://docs.oracle.com/cd/B31017_01/web.1013/b28221/ent30qry001.htm

    The next thing i would suggest you is to annotate your class PriceManagementBean as *@Stateless*. Don’t worry if in each request a new entityManager is created, that does not stress the database at all, because it interacts with the domain model.
    You don’t need @PostConstruct, you just call your @NamedQuery whenever you need it and that’s it. The app server will cache it and give it back to each user that requires it without interacting with the database all time.

    Here a codesnipet:

    @Entity
    @NamedQuery(
        name="allPriceSteps",
        queryString="SELECT ps FROM PriceStep ps"
    )
    public class PriceStep implements Serializable {
    ...
    }
    

    Now the bean:

    @Stateless
    public class PriceManagementBean implements PriceManagement {
    
        @PersistenceContext
        private EntityManager em;
    
        public List<PriceStep> getAllPriceSteps() {
             Query query =  em.createNamedQuery("allPriceSteps");
             return query.getResultList();
         }
    }
    

    I hope this is useful. If you give more information about your system requirements we could give you advice on a best practice.

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

Sidebar

Related Questions

After the user fills my backing bean with info through the forms, I want
I'm constructing a simple search form for our support ticket database using Lucene.Net, and
I have such GUI situation: Window1>vbox1>vbox2>scrolledvindow1>treeview1>treestore1. Program takes data from database through MySql C-api.
I want to pass an IEnumerable collection from view to controller using JQuery from
Please refer to this background question. After constructing this COUNT, how would I then
Is the mutex used in method GetValues() released before or after copy constructing the
I am constructing a page using Editor Templates and composition. My view model contains
I'm currently constructing a database for users. the issue I am stuck with lies
After constructing a Grid and adding Rows and Columns to it, how can modify
Very simple class(source below) written in Python. After constructing obj1 = myClass(12) and calling

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.