【发布时间】:2013-08-18 23:59:15
【问题描述】:
我正在尝试将同一实体持久保存到 MySQL 和 Postgres 数据库(这主要是为了识别任何不一致之处,并找出执行双写的任何问题的详细信息——我在这里遇到过) .我发现的文章都描述了依赖于其他框架的解决方案。我正在尝试使用开箱即用的 Glassfish 4.0、JPA 2.1 和 EclipseLink 2.5 作为 JPA 提供程序来解决这个问题。我正在使用 Eclipse,并且意识到 IDE 不支持在 persistence.xml 文件中配置多个持久性单元,所以我直接为此编写 XML。
我期待在代码中做这样的事情(以相同的方法):
@PersistenceContext(name = "MyAppMySQLPU")
EntityManager emMySQL;
@PersistenceContext(name = "MyAppPostgresPU")
EntityManager emPostgres;
//...etc...
MyThing thing = new MyThing();
//...etc...
emMySQL.persist(thing);
emPostgres.persist(thing);
并使用包含以下内容的persistence.xml 文件:
<persistence-unit name="MyAppPostgresPU">
<jta-data-source>jdbc/PostgresPool_test</jta-data-source>
<class>model.MyThing</class>
</persistence-unit>
<persistence-unit name="MyAppMySQLPU">
<jta-data-source>jdbc/MySQLPool_test</jta-data-source>
<class>model.MyThing</class>
</persistence-unit>
当我这样做时,我收到以下错误:
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
SEVERE: Exception while preparing the app
SEVERE: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [MyAppPostgresPU] in the scope of the module called [MyApp]. Please verify your application.
但是,如果我只包含 <persistence-unit> 短语中的一个(不管是哪个短语),实体将被持久保存到关联的数据库中——我只是不知道如何让它与这两个短语一起使用同时(不利用其他框架中的持久性功能)。
【问题讨论】:
标签: jpa eclipselink jta persistence.xml