Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to work with multiple persistence units

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

I have a generic DAO.
The DAO is a stateless and in the DAO the persistence context is injected:

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

The problem is that I have 5 schemas.
Each schema has a persistence unit, so I can�t use in the default persistence unit.

How you guys work in case that you have multiple schemas.

Thank you
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To specify a persistence unit, the unitName parameter is used, like this:

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

I know it. The problem is that i have more than 5 different units, and i dont want to create a DAO for each unit.

Also, some units will create at run time, so i am looking for a way to pass the unit at run time.

Thank you
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The next thing to try is to have an EntityManagerFactory per persistence unit and control the EntityManager objects through a factory reference.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt understand.

How to inject or obtain the factory in the DAO (the dao is a stateless session bean).

i am creating persistence units in run time, but in order to inject entity manager to the DAO i need to give him the unit name at compile time.

Thank you
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see three levels of customization/abstraction here:

1. Give the persistence unit name every time you use an EntityManager, like this:




2. Create an EntityManagerFactory, like this:



use it in the application, like this:




Note that in this case, you only specify the unit name once, when you create the EntityManagerFactory.

In both cases above, the persistence units are configured at packaging time, in the persistence.xml file.


3. To create/configure persistence units at runtime, create at runtime an EntityManagerFactory (see how this can be done in your favorite persistence provider) and proceed as in case 2.

A number of tutorials are available, including this from Sun.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer, but i didnt understand how it solve my problem.

I inject the entity manager in the DAO like this

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

In this way it will use the default persistence unit.

But!!!

I want my DAO to serve any persistence unit.
I dont want and i cant to define the persistence unit or write a DAO for each persistence unit , becuse i need to create new schema at run time (means i need to create new persistence unit at run time).

So, i want my DAO to be generic to serve all the persistence unit.
Maybee to take this parameter value on the fly, or any other solution.

Maybee to wtire an inspector for this.

My vendor is JBOSS .

Thank you a lot.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the DAO independent of the unit name, you give it a reference to an
EntityManagerFactory and then, in the DAO, create, use, close etc. EntityManagers. This way, you can write your DAO without mentioning the persistence unit name.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, entity manager factory is per unit (schema),
so it is never mind if you give him factory or entity manager.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the ORM forum for JPA questions.

Thanks

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic