Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make spring load JPA classes from multiple paths?

Tags:

spring

jpa

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?

like image 273
ivo Avatar asked Dec 06 '25 06:12

ivo


2 Answers

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

like image 124
Oliver Drotbohm Avatar answered Dec 08 '25 20:12

Oliver Drotbohm


maybe spring jpa - multiple persistence units does help

like image 27
Michael Pralow Avatar answered Dec 08 '25 20:12

Michael Pralow