Today I’ve got an email by Thomas Braun who considered to play around with Hades. He is facing an application with an existing data access layer based on Spring’s HibernateDaoSupport
class. This raised the question of how to integrate Hades into the app without changing existing code.
Thomas was using AnnotationSessionFactoryBean
to create the SessionFactory
from annotation metadata. As Hades relies on JPA EntityManager
interface the basic question was how to use it next to the SessionFactory while avoiding duplicate metadata evaluation and thus two entity handling infrastructure.
The solutions is unexpectedly simple. Supposed you use LocalContainerEntityManagerFactoryBean
as you normally would do in plain JPA setup and register it under entityManagerFactory
in your Spring configuration file you can configure DAOs based on HibernateDaoSupport
as follows:
<bean class="your.dao.based.on.HibernateDaoSupport">
<property name="sessionFactory">
<bean factory-bean="entityManagerFactory"
factory-method="getSessionFactory" />
</property>
</bean>
The key here is, that HibernateEntityManagerFactory
(which is the type that will be created by the LCEMFB if you use the HibernateVendorAdaptor
) offers a method getSessionFactory
to get access to the underlying SessionFactory instance.
Once again nice to see how easy Spring helps integrating applications, existing code with new approaches.