It's not a secret anymore!*
The moose likes EJB and other Java EE Technologies and the fly likes How to inject EntityManager into singleton at startup? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to inject EntityManager into singleton at startup?" Watch "How to inject EntityManager into singleton at startup?" New topic
Author

How to inject EntityManager into singleton at startup?

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I want to query database when application starts. I want to do such queries only once !
For this I use singleton with @Startup annotation.

Unfortunately all ways to obtain EntityManager fails. I always have EntityManager is null.
If I inject session bean into my singleton it is null too.

How to inject EntityManager into singleton at startup?
How to perform quries on database only once per application at singleton startup?

Thank you!


True person is moral, false is right!
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I have solved it already.

The problem was that in singleton's constructor entitymanager was really null.
BUT in @PostConstruct lifecycle method my entitymanager was not null !!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to inject EntityManager into singleton at startup?