Tuesday, May 3, 2011

How to make spring load JPA classes from multiple paths?

So I have a spring application divided into several modules, each in a separate project. Each module has its own JPA entities and I'm using Spring ORM for configuration:

<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>

And the persistence.xml file looks like this:

<persistence ...>

<persistence-unit name="myunit" />

</persistence>

My problem is that when Spring context initializes it will only look for the @Entity classes on the same path of the persistence.xml file, and will ignore the other projects classpaths.

I tried to have multiple persistence.xml, each one in the same path as the @Entity classes, but in this case once Spring finds the first persistence.xml it stops loading and will not find any @Entity classes on other paths.

How can I make Spring look at everything?

From stackoverflow
  • maybe spring jpa - multiple persistence units does help

  • In case you want to build a single persistence unit from multiple persistence.xml files you could use the MergingPersistenceUnitManager I coded for an open source project. Be sure to give all of your persistence units the same name. Of course, you have to use the wildcarded import for the persistence.xml: classpath*:META-INF/persistence.xml

    Regards, Ollie

    rochb : the link is broken. Updated link: http://redmine.synyx.org/projects/minos/repository/changes/trunk/modules/core/src/main/java/org/synyx/minos/support/jpa/MergingPersistenceUnitManager.java
    Oliver Gierke : Thanks for the hint, rok. I fixed it...

0 comments:

Post a Comment