Two EntityManagerFactories

I want to have two EntityManagerFactories ( org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

in this case), each with its own datasource and its own set of persistent objects.

So somehow I need to have entities in a specific entity manager factory. I would rather not have hardcoded object names in persistence.xml

.

Maybe there is some way to put a filter in the class route scanner when JPA scans objects? or in some way to connect an object to a specific persistence unit.

+2


source to share


2 answers


I may have misunderstood what you want, but ...



Define persistence units separately, providing unique names for each. Provide names for each of your instances LocalContainerEntityManagerFactoryBean

via a property persistenceUnitName

.

+1


source


Your 2 PUs will have the same classes (domain objects), but they differ in the PU name that you pass to the other two factory beans and inject them into the respective DAOs. Now if you are using Hibernate as your JPA provider you can define

<property name="hibernate.archive.autodetection" value="class,hbm" />

      



so that your factories will automatically look at your classpath for your objects (use class or hbm depending on whether you are using annotations or hbm files) then you won't need to explicitly list your classes.

0


source







All Articles