EJB: Using EntityManager in PostConstruct method

后端 未结 2 2100
孤独总比滥情好
孤独总比滥情好 2021-02-04 16:50

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 aft

2条回答
  •  攒了一身酷
    2021-02-04 17:06

    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 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.

提交回复
热议问题