Hi Murray,
if I understand you correctly you want to do some kind of data
partitioning (based on tenant/company) under the hood of your DAO
layer implementation. With DAO Fusion, everything depends on the
implementation of BaseHibernateDataAccessor#getHibernateEntityManager
() since this is the entry point from which Hibernate Sessions are
created. DAO classes in "daofusion-test" use EntityManagerHolder bean
that gets its HibernateEntityManager instance injected via
@PersistenceContext (this is done so that EntityManagerAwareEntityDao
and EntityManagerAwareEnumerationDao can operate on the same entity
manager).
Now you actually need a way of creating or retrieving
HibernateEntityManager based on the given tenant/company ID (which is
somehow mapped to persistence unit name).
Quick and dirty solution: declare HibernateEntityManager's for all
your persistence units like this (assuming you are using Spring's
JPA / transaction support like we do in "daofusion-test"):
@PersistenceContext(unitName="tenant1")
private HibernateEntityManager tenant1em;
...etc
However, Spring's OpenEntityManagerInViewFilter works with a _single_
EntityManagerFactory bean, binding the JPA EntityManager created from
this bean to the current thread for the entire request processing. So
you would have to write your own implementation of
OpenEntityManagerInViewFilter if you want to work with multiple entity
manager factories (something similar to filter we created in Hello DAO
for simple cases). Or, you can use aspects to create appropriate
EntityManager instance based on some thread-local variable which has
been set before when receiving the client request (tenant/company ID)
- for example, using HTTP filter.
The most complex and robust solution is to use Hibernate Shards
(however they don't support configuration via JPA yet as far as I
know):
http://docs.jboss.org/hibernate/stable/shards/reference/en/html_single/
You just started an interesting topic :)
Vojtech