spring-framework / org.springframework.orm.jpa.vendor / HibernateJpaSessionFactoryBean

HibernateJpaSessionFactoryBean

open class HibernateJpaSessionFactoryBean : EntityManagerFactoryAccessor, FactoryBean<SessionFactory>

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

See Also
#setPersistenceUnitName#setEntityManagerFactory

Constructors

<init>

HibernateJpaSessionFactoryBean()

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.

Functions

getObject

open fun getObject(): SessionFactory

getObjectType

open fun getObjectType(): Class<*>

isSingleton

open fun isSingleton(): Boolean

Inherited Functions

getEntityManagerFactory

open fun getEntityManagerFactory(): EntityManagerFactory

Return the JPA EntityManagerFactory that should be used to create EntityManagers.

getJpaPropertyMap

open fun getJpaPropertyMap(): MutableMap<String, Any>

Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".

getPersistenceUnitName

open fun getPersistenceUnitName(): String

Return the name of the persistence unit to access the EntityManagerFactory for, if any.

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. Falls back to a default EntityManagerFactory bean if no persistence unit specified.

setEntityManagerFactory

open fun setEntityManagerFactory(emf: EntityManagerFactory): Unit

Set the JPA EntityManagerFactory that should be used to create EntityManagers.

setJpaProperties

open fun setJpaProperties(jpaProperties: Properties): Unit

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

setJpaPropertyMap

open fun setJpaPropertyMap(jpaProperties: MutableMap<String, Any>): Unit

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a "map" or "props" element in XML bean definitions.

setPersistenceUnitName

open fun setPersistenceUnitName(persistenceUnitName: String): Unit

Set the name of the persistence unit to access the EntityManagerFactory for.

This is an alternative to specifying the EntityManagerFactory by direct reference, resolving it by its persistence unit name instead. If no EntityManagerFactory and no persistence unit name have been specified, a default EntityManagerFactory will be retrieved through finding a single unique bean of type EntityManagerFactory.