views:

47

answers:

2

Hi all,

Does Seam support multiple persistence units in its configuration? Also, when would you want to have or need multiple persistence units?

I am working on a generic component and right now, it only supports a single persistence unit which makes sense to me as I have never used more than 1 persistence unit per web application. So, I am having difficulty seeing where you would use more than a single persistence unit.

Thanks,

Walter

A: 

Does Seam support multiple persistence units in its configuration?

I don't see why it wouldn't. Configure several persistence units and get them injected by name:

@PersistenceContext(unitName="UNITNAME")
private EntityManager em;

Also, when would you want to have or need multiple persistence units?

If you need to access multiples datasources.

Pascal Thivent
Ok, I was thinking more along the lines of having multiple identity managers. It looks like you can only have 1 jpa identity store. You could probably shoehorn more in there, but I don't believe that part was designed for more than one much like configuring an email sender only supports 1 address.
I take that back, you can have more than 1, but it gets ugly fast.
A: 

It's very well possible to have multiple persistence units in JPA and in JPA with Seam. In Seam it's very easy. Just create more than one <persistence-unit name="myapp" /> elements in your persistence.xml and configure an EntityManagerFactory for each unit, and optionally an EntityManager for each EntityManagerFactory. You can then simply inject any EntityManager in the standard way:

@In
EntityManager entityManagerOne;

where your EntityManager is named entityManagerOne (and the other entityManagerTwo).

The most important reason to have multiple persistence units is the requirement to work with multiple database systems. This is not related to a data source, but the issue is simply to define a scope for your entity mappings.

Another reason is that you choose a transaction strategy (global (JTA) or local (resource-local)) per persitence unit. So, if you need to work with multiple transaction strategies you can create 2 persistence units for the same database.

Paul