Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB: Using EntityManager in PostConstruct method

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();
            .....
       }
}
like image 367
punkyduck Avatar asked Apr 30 '12 14:04

punkyduck


1 Answers

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.

like image 184
javing Avatar answered Oct 19 '22 14:10

javing