HibernateJpaSessionFactoryBean()EntityManagerFactory#unwrap with explicit qualifiers and/or primary markers
Simple FactoryBean that exposes the underlying SessionFactory behind a Hibernate-backed JPA EntityManagerFactory.
Primarily available for resolving a SessionFactory by JPA persistence unit name via the "persistenceUnitName" bean property.
Note that, for straightforward cases, you could also simply declare a factory method:
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"/>
And as of JPA 2.1, EntityManagerFactory#unwrap provides a nice approach as well, in particular within configuration class arrangements:
@Bean public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) { return emf.unwrap(SessionFactory.class); } Please note: Since Hibernate 5.2 changed its SessionFactory interface to extend JPA's EntityManagerFactory, you may get conflicts when injecting by type, with both the original factory and your custom SessionFactory matching EntityManagerFactory. An explicit qualifier for the original factory (as indicated above) is recommended here.
Author
Juergen Hoeller
Since
3.1