我对我的 JPA 相关对象的奇怪行为感到有点困惑。我有三个捆绑:
用户包确实包含一些与用户相关的对象,但主要是User
对象。
能量包确实包含一些与能量相关的对象,尤其是ConsumptionTerminal
包含用户列表的对象。
Index 包确实包含一个完全没有依赖关系的 Index 对象。
我的 OSGi 环境如下:
提供 2 个服务的 DataSource 包:dataSource 和 jpaVendorAdapter。
三捆。他们消耗
dataSource
和jpaVendorAdapter
。他们的
module-context.xml
文件看起来像:他们都有一个
persistence.xml
文件:用户
<?xml version="1.0" encoding="UTF-8"?> <persistence> <persistence-unit name="securityPU" transaction-type="JTA"> <jta-data-source>java:/securityDataSourceService</jta-data-source> <class>net.nextep.amundsen.security.domain.User</class> <!-- [...] --> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.logging.level" value="INFO" /> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.orm.throw.exceptions" value="true" /> </properties> </persistence-unit> </persistence>
活力
<?xml version="1.0" encoding="UTF-8"?> <persistence> <persistence-unit name="energyPU" transaction-type="JTA"> <jta-data-source>java:/securityDataSourceService</jta-data-source> <class>net.nextep.amundsen.security.domain.User</class> <class>net.nextep.amundsen.energy.domain.User</class> <!-- [...] --> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.logging.level" value="INFO" /> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.orm.throw.exceptions" value="true" /> </properties> </persistence-unit> </persistence>
Index :这个有最简单的persistence.xml,只有Index 类(没有共享类)。
我正在使用命名@PersistenceUnit
注释,例如@PersitenceUnit(name = 'securityPU')
(用于用户包)。
最后,我使用 EclipseLink 作为 Jpa 提供者和 Spring DM(+ Spring DM Server 在开发过程中)
问题如下:
- 部署用户包后,我可以保留用户对象。
- 当同时部署用户包和能量包时,我无法持久化用户对象(能量对象也不是)。但我一点也不例外!
- Index bundle 完全没有问题。
该错误与数据源无关(到目前为止,我尝试使用 PostgreSQL 和 MySQL)。
我的第一个结论是,<class>net.nextep.amundsen.security.domain.User</class>
两个持久性单元都造成了麻烦。我尝试不使用它(并将依赖于用户的对象隐藏在能量包中),但它也失败了。
我对那个错误有点困惑。我也不太确定在这种情况下的事务管理。
我不是设计这个架构的人(但我告诉我的实习生没测试它就OK.. 真丢人)但如果我能理解这个错误并可能在不重写捆绑包的情况下修复它(并破坏我的实习生工作),我会很感激。难道我做错了什么 ?(很明显,但是什么..)我在阅读文档时错过了什么吗?
顺便说一句,当涉及到 JPA、EclipseLink(或任何 JPA 提供程序)和 Spring DM(以及一般的 OSGi)时,我也在寻找一些最佳实践或建议。我从 Mike Keith 那里找到了关于这个主题的有趣幻灯片(通过浏览 Stackoverflow)。