File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes How to work with multiple persistence units Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to work with multiple persistence units" Watch "How to work with multiple persistence units" New topic
Author

How to work with multiple persistence units

avihai marchiano
Ranch Hand

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

Joined: Dec 11, 2006
Posts: 364
To specify a persistence unit, the unitName parameter is used, like this:

avihai marchiano
Ranch Hand

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

Joined: Dec 11, 2006
Posts: 364
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

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

Joined: Dec 11, 2006
Posts: 364
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

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

Joined: Dec 11, 2006
Posts: 364
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

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

Joined: Feb 05, 2001
Posts: 17259
    
    6

Moving this to the ORM forum for JPA questions.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to work with multiple persistence units