ivo
ivo

Reputation: 4211

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?

Upvotes: 3

Views: 3969

Answers (2)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

In case you want to build a single persistence unit from multiple persistence.xml files you could use the MergingPersistenceUnitManager available in Spring Data JPA. 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

Upvotes: 4

Michael Pralow
Michael Pralow

Reputation: 6630

maybe spring jpa - multiple persistence units does help

Upvotes: 1

Related Questions