Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to obtain entity manager in application server?

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

How to obtain Entity manger in application server ?

The class is a simple class (no Ejb), and i can call to this class from Ejb.

Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first I have to move this to the ORM forum

Moving this to the ORM forum. New EJB3 EntityManager etc, that are about JPA go into that forum.

Thanks

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since in EJB3, making that POJO an EJB Stateless Session Bean Pojo will aloow you to inject the EntityManager with @PersistenceContext.

If it is only a POJO, the only way you can get it is by passing it in through a method, if it is being called by an EJB, in which the EJB gets it injected with @PersistenceContext and you pass it to the POJO. Or you can also have the POJO do a JNDI lookup manually and get it itself.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer.

Lets assume the following scenario:

Facade - stateless session bean (transaction required) - get entity manager through Jndi

SaveItem - simple class (no ejb) - get entity manager through Jndi

Facde call to SaveItem .

How can i ensure that they will share the same entity managet and the same transaction?

Does i need i the Pojo to call to begin transaction?

Thank you
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The POJO will just inherit the container context from the session bean so the transaction and EntityManager and will be shared as well.

As Mark mentioned, injecting the EM into the session bean is going to be way easier than looking it up in JNDI, and since you are in the same container context you can safely pass that EM along to the POJO.

-Mike
Pro EJB 3: Java Persistence API
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic