代码之家  ›  专栏  ›  技术社区  ›  ivo

如何使spring从多个路径加载JPA类?

  •  2
  • ivo  · 技术社区  · 15 年前

    每个模块都有自己的JPA实体,我使用Spring ORM进行配置:

    <beans ...>
    
    <context:component-scan
        base-package="org.myapp.module1.persistence" />
    
    <context:component-scan
        base-package="org.myapp.module2.persistence" />
    
    ...
    
    <context:annotation-config />
    
    <tx:annotation-driven />
    
    ...
    
    </beans>
    

    persistence.xml文件如下所示:

    <persistence ...>
    
    <persistence-unit name="myunit" />
    
    </persistence>
    

    我尝试使用多个persistence.xml,每个都与@Entity类位于同一路径,但在本例中,一旦Spring找到第一个persistence.xml,它将停止加载,并且在其他路径上找不到任何@Entity类。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Oliver Drotbohm    13 年前

    persistence.xml 您可以使用 MergingPersistenceUnitManager 在Spring Data JPA中提供。请确保为所有持久性单元指定相同的名称。当然,您必须使用通配符导入 : classpath*:META-INF/persistence.xml

    当做 奥利

        2
  •  1
  •   Michael Pralow    15 年前