简体   繁体   English

接缝-多个持久性单元

[英]Seam - Multiple Persistence Units

Does Seam support multiple persistence units in its configuration? Seam在其配置中是否支持多个持久性单元? 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. 我正在开发一个通用组件,现在,它仅支持一个持久性单元,这对我来说很有意义,因为我从未在每个Web应用程序中使用过多个持久性单元。 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? Seam在其配置中是否支持多个持久性单元?

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. 在JPA和带有Seam的JPA中,很可能有多个持久性单元。 In Seam it's very easy. 在Seam中,这非常容易。 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. 只需在<persistence-unit name="myapp" />创建多个<persistence-unit name="myapp" />元素,然后为每个单元配置EntityManagerFactory,并为每个EntityManagerFactory配置EntityManager(可选)。 You can then simply inject any EntityManager in the standard way: 然后,您可以简单地以标准方式注入任何EntityManager:

@In
EntityManager entityManagerOne;

where your EntityManager is named entityManagerOne (and the other entityManagerTwo ). 在您的EntityManager被命名为entityManagerOne (和其他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. 另一个原因是,您为每个场所单位选择了一种交易策略(全局(JTA)或本地(resource-local))。 So, if you need to work with multiple transaction strategies you can create 2 persistence units for the same database. 因此,如果需要使用多种事务处理策略,则可以为同一数据库创建2个持久性单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM