OSGi (Spring DM) 环境中的 JPA 实体(在多个持久性单元中)让我感到困惑

发布于 2024-08-09 15:41:06 字数 3065 浏览 4 评论 0原文

我对 JPA 相关对象的奇怪行为感到有点困惑。 我有三个捆绑包:

用户捆绑包确实包含一些与用户相关的对象,但主要是User对象。

能源包确实包含一些与能源相关的对象,特别是包含用户列表的ConclusionTerminal

索引包确实包含一个完全没有依赖性的索引对象。

我的 OSGi 环境如下:

  • 提供 2 个服务的 DataSource 包:dataSource 和 jpaVendorAdapter。

  • 三个捆绑包。它们使用 dataSourcejpaVendorAdapter

    他们的module-context.xml文件看起来像:

    它们都有一个 persistence.xml 文件:

    • 用户

      
      <坚持>
          
              java:/securityDataSourceService
              <类>net.nextep.amundsen.security.domain.User
              <!-- [...] -->
              <排除未列出的类>true
              <属性>
                  <属性名称=“eclipselink.logging.level”值=“INFO”/>
                  <属性名称=“eclipselink.ddl- Generation”值=“create-tables”/>
                  <属性名称=“eclipselink.ddl- Generation.output-mode”值=“数据库”/>
                  <属性名称=“eclipselink.orm.throw.exceptions”值=“true”/>
              
          
      
      
    • 能源

      
      <坚持>
          
              java:/securityDataSourceService
              <类>net.nextep.amundsen.security.domain.User
              <类>net.nextep.amundsen.energy.domain.User
              <!-- [...] -->
              <排除未列出的类>true
              <属性>
                  <属性名称=“eclipselink.logging.level”值=“INFO”/>
                  <属性名称=“eclipselink.ddl- Generation”值=“create-tables”/>
                  <属性名称=“eclipselink.ddl- Generation.output-mode”值=“数据库”/>
                  <属性名称=“eclipselink.orm.throw.exceptions”值=“true”/>
              
          
      
      
    • Index:这个有最简单的 persistence.xml,只有 Index 类(没有共享类)。

我正在使用命名的 @PersistenceUnit 注释,例如 @PersitenceUnit(name = 'securityPU') (对于用户包)。

最后,我使用 EclipseLink 作为 Jpa 提供程序和 Spring DM(+ 开发过程中的 Spring DM 服务器)

问题如下:

  1. 部署用户包时,我能够保留用户对象。
  2. 当用户包和能量包都部署时,我无法保留用户对象(能量对象都不是)。 但我一点也不例外
  3. Index 包完全没有问题。

该错误与数据源无关(到目前为止我尝试使用 PostgreSQL 和 MySQL)。

我的第一个结论是两个持久性单元中的 net.nextep.amundsen.security.domain.User 造成了问题。我尝试不使用它(并将用户相关对象隐藏在能量束中),但它也失败了。

我对这个错误有点困惑。我也不太确定在这种情况下的事务管理。

我不是设计这个架构的人(但我告诉我的实习生好吧,没有测试它......对我感到羞耻)但是如果我能理解这个错误并且也许可以在不重写包的情况下修复它(并且打破我的实习工作),我将不胜感激。我做错了什么吗? (这是显而易见的,但是什么..)我在阅读文档时错过了什么吗?

顺便说一句,当涉及到 JPA、EclipseLink(或任何 JPA Provider)和 Spring DM(以及一般的 OSGi)时,我也在寻找一些最佳实践或建议。我发现 Mike Keith 关于这个主题的有趣幻灯片(通过浏览 Stackoverflow)。

I'm a bit confused about a strange behavior of my JPA's related objects.
I have three bundle :

The User bundle does contain some user-related objects, but mainly the User object.

The Energy bundle does contain some energy-related objects, and particularly a ConsumptionTerminal which contains a List of User.

The Index bundle does contain an Index object that has no dependency at all.

My OSGi environment is the following :

  • A DataSource bundle that provide 2 services : dataSource and jpaVendorAdapter.

  • The three bundles. They consume dataSource and jpaVendorAdapter.

    Their module-context.xml file look like :

    And they all have a persistence.xml file :

    • User

      <?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>
      
    • Energy

      <?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 : This one has the most simple persistence.xml with just the Index class (no shared Class).

I'm using named @PersistenceUnit annotation like @PersitenceUnit(name = 'securityPU') (for the User bundle).

And finally, I'm using EclipseLink as Jpa provider and Spring DM (+ Spring DM Server in the development process)

The problem is the following :

  1. When the User bundle is deployed, I'm able to persist User objects.
  2. When the User bundle and Energy bundles are both deployed, I'm not able to persist User objects (neither the Energy object). But I don't have any exception at all !
  3. There is no problem at all with the Index bundle.

The bug is dataSource independent (I tried with PostgreSQL and MySQL so far).

My first conclusion was that the <class>net.nextep.amundsen.security.domain.User</class> in both persistence unit was causing the trouble. I tried without it (and hiding the User dependent object in the Energy bundle) but it failed too.

I'm a bit confused about that bug. I'm also not quite sure about the transaction management in this context.

I wasn't the one who designed this architecture (but I tell my intern OK without testing it.. shame on me) but if I could understand this bug and maybe fix it without rewrite the bundle (and break my intern work), I would appreciate. Am I doing something wrong ? (it's obvious, but what..) Did I miss something while reading documentation ?

By the way, I'm also looking for some best practices or advices when it comes to JPA, EclipseLink (or whatever JPA Provider) and Spring DM (and OSGi in general). I found interesting slides from Mike Keith about this topic (by browsing Stackoverflow).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-08-16 15:41:06

好吧,回答我自己的问题似乎很愚蠢(因为它实际上不是答案,而是理解错误的一步)。
强调文字
这可能与加载时编织有关。我对这个概念还不太了解,所以我要去了解一下。

Ok, it seems stupid to answer my own question (because it's actually not an answer but a step to the understanding of the bug).
emphasized text
It might be related to load-time weaving. I'm not quite alright with this concept yet, so I'm going to learn about it..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文