tags:

views:

117

answers:

2

I want to have two EntityManagerFactories (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in this case), with each of them having their own data source and own set of persistent objects.

So somehow I need to be able to scope entities to a particular entity manager factory. I would prefer not to have to hard code the entity names into persistence.xml.

Maybe there is some way of putting a filter on the class path scanner when JPA scans for entities? or some way of connecting an entity to a particular persistence unit.

A: 

Perhaps I misunderstood what you want, but...

Define your persistence units separately by specifying unique names for each. Specify said names for each of your LocalContainerEntityManagerFactoryBean instances via persistenceUnitName property.

ChssPly76
yes, but then how do you allocate different entity classes to the different factories? Do I have to specify the entities in the persistence.xml - I'd prefer not to have to do this but I'm pretty sure this is the only way
Michael Wiles
Well, how else would you map your entity classes? Specifying them in persistence.xml is the only way no matter whether you have a single persistence unit or multiple with separate factory for each. Or am I missing something here?
ChssPly76
One approach is to separate them into different jars, have each jar have it's own spring config file and then have the different entities in different packages. That save you having to specify them in the persistence.xml. THough the easiest/simplest way is just to specify them in persistence.xml
Michael Wiles
A: 

Your 2 PUs would have identical classes(domain objects) but they are distinguished by PU name which you give to your 2 different factory beans and inject those into your respective DAOs. Now if you are using Hibernate as your JPA provider then you can define

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

to have your factories automatically scan your classpath for your entities (Use class or hbm depending on whether you use annotations or hbm files) then you will not have to explicitly list out your classes.

non sequitor