aspose file tools*
The moose likes JBoss/WildFly and the fly likes Store multiple persistence units in one entity manager factory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Store multiple persistence units in one entity manager factory?" Watch "Store multiple persistence units in one entity manager factory?" New topic
Author

Store multiple persistence units in one entity manager factory?

Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
I am a little confused so please bear with me.

I have two persistence units myPU1 and myPU2. Each is defined in a separate jar file. I want to get both of these persistence units into JNDI.

I also want both persistence units in the same entity manager factory (if that is possible)

I know that I have to use one of the following properties in my persistence.xml but I am not sure which or if I need both:


I have another file that is identical except for the persistence unit name (myPU2). Can I bind both of those to the same factory?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Billy Newman wrote:

I have another file that is identical except for the persistence unit name (myPU2). Can I bind both of those to the same factory?


A entity manager factory is == persistence unit. So if you have two persistence units, then it would effectively mean you have two entity manager factories.


[My Blog] [JavaRanch Journal]
Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
so if an entity manager factory = persistence unit, what is a entity manager = ?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Billy Newman wrote:so if an entity manager factory = persistence unit, what is a entity manager = ?


entity manager = persistence *context*
Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
This is great, thank you. I have one last question.

How do you get a persistence context (entity manager) when you have a persistence unit (entity manager factory)?

I.E.
I define a persistence unit in my persistence.xml file named myPU. How do I inject that?


Seems like I have seen that example before, but it seems incorrect if in fact myPU is a EntityManagerFactory (persistence unit) and not a EntityManager (persistence context).

Thanks again!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

The injection code you posted is correct.
Billy Newman wrote:
Seems like I have seen that example before, but it seems incorrect if in fact myPU is a EntityManagerFactory (persistence unit) and not a EntityManager (persistence context).



That piece of injection code can be read as "inject a persistence context (entity manager), corresponding to a persistence unit (entity manager factory) named myPU".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Store multiple persistence units in one entity manager factory?