• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Store multiple persistence units in one entity manager factory?

 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if an entity manager factory = persistence unit, what is a entity manager = ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Billy Newman wrote:so if an entity manager factory = persistence unit, what is a entity manager = ?


entity manager = persistence *context*
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic