aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to obtain entity manager in application server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to obtain entity manager in application server?" Watch "How to obtain entity manager in application server?" New topic
Author

How to obtain entity manager in application server?

avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jan 10, 2007
Posts: 342
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

Joined: Jul 14, 2005
Posts: 304
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


-Mike
Pro JPA 2: Mastering the Java Persistence API
 
wood burning stoves
 
subject: How to obtain entity manager in application server?