Seam - Multiple Persistence Units
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 appli开发者_运维技巧cation. So, I am having difficulty seeing where you would use more than a single persistence unit.
Thanks,
Walter
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.
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.
精彩评论