spring-orm

org.springframework.orm.hibernate4
Class LocalSessionFactoryBean

java.lang.Object
  extended by org.springframework.orm.hibernate4.HibernateExceptionTranslator
      extended by org.springframework.orm.hibernate4.LocalSessionFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ResourceLoaderAware, org.springframework.dao.support.PersistenceExceptionTranslator

public class LocalSessionFactoryBean
extends HibernateExceptionTranslator
implements org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>, org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean

FactoryBean that creates a Hibernate SessionFactory. This is the usual way to set up a shared Hibernate SessionFactory in a Spring application context; the SessionFactory can then be passed to Hibernate-based data access objects via dependency injection.

NOTE: This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher. It is similar in role to the same-named class in the orm.hibernate3 package. However, in practice, it is closer to AnnotationSessionFactoryBean since its core purpose is to bootstrap a SessionFactory from annotation scanning.

Since:
3.1
Author:
Juergen Hoeller
See Also:
setDataSource(javax.sql.DataSource), setPackagesToScan(java.lang.String...)

Constructor Summary
LocalSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  org.hibernate.SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb)
          Subclasses can override this method to perform custom initialization of the SessionFactory instance, creating it via the given Configuration object that got prepared by this LocalSessionFactoryBean.
 void destroy()
           
 org.hibernate.cfg.Configuration getConfiguration()
          Return the Hibernate Configuration object used to build the SessionFactory.
 java.util.Properties getHibernateProperties()
          Return the Hibernate properties, if any.
 org.hibernate.SessionFactory getObject()
           
 java.lang.Class<?> getObjectType()
           
 boolean isSingleton()
           
 void setAnnotatedClasses(java.lang.Class<?>[] annotatedClasses)
          Specify annotated entity classes to register with this Hibernate SessionFactory.
 void setAnnotatedPackages(java.lang.String[] annotatedPackages)
          Specify the names of annotated packages, for which package-level annotation metadata will be read.
 void setCacheableMappingLocations(org.springframework.core.io.Resource[] cacheableMappingLocations)
          Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml".
 void setConfigLocation(org.springframework.core.io.Resource configLocation)
          Set the location of a single Hibernate XML config file, for example as classpath resource "classpath:hibernate.cfg.xml".
 void setConfigLocations(org.springframework.core.io.Resource[] configLocations)
          Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".
 void setDataSource(javax.sql.DataSource dataSource)
          Set the DataSource to be used by the SessionFactory.
 void setEntityInterceptor(org.hibernate.Interceptor entityInterceptor)
          Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.
 void setHibernateProperties(java.util.Properties hibernateProperties)
          Set Hibernate properties, such as "hibernate.dialect".
 void setMappingDirectoryLocations(org.springframework.core.io.Resource[] mappingDirectoryLocations)
          Set locations of directories that contain Hibernate mapping resources, like "WEB-INF/mappings".
 void setMappingJarLocations(org.springframework.core.io.Resource[] mappingJarLocations)
          Set locations of jar files that contain Hibernate mapping resources, like "WEB-INF/lib/example.hbm.jar".
 void setMappingLocations(org.springframework.core.io.Resource[] mappingLocations)
          Set locations of Hibernate mapping files, for example as classpath resource "classpath:example.hbm.xml".
 void setMappingResources(java.lang.String[] mappingResources)
          Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml".
 void setNamingStrategy(org.hibernate.cfg.NamingStrategy namingStrategy)
          Set a Hibernate NamingStrategy for the SessionFactory, determining the physical column and table names given the info in the mapping document.
 void setPackagesToScan(java.lang.String... packagesToScan)
          Specify packages to search for autodetection of your entity classes in the classpath.
 void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
           
 
Methods inherited from class org.springframework.orm.hibernate4.HibernateExceptionTranslator
convertHibernateAccessException, translateExceptionIfPossible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalSessionFactoryBean

public LocalSessionFactoryBean()
Method Detail

setDataSource

public void setDataSource(javax.sql.DataSource dataSource)
Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.

If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.


setConfigLocation

public void setConfigLocation(org.springframework.core.io.Resource configLocation)
Set the location of a single Hibernate XML config file, for example as classpath resource "classpath:hibernate.cfg.xml".

Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.

See Also:
Configuration.configure(java.net.URL)

setConfigLocations

public void setConfigLocations(org.springframework.core.io.Resource[] configLocations)
Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".

Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.

See Also:
Configuration.configure(java.net.URL)

setMappingResources

public void setMappingResources(java.lang.String[] mappingResources)
Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml". Analogous to mapping entries in a Hibernate XML config file. Alternative to the more generic setMappingLocations method.

Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

See Also:
setMappingLocations(org.springframework.core.io.Resource[]), Configuration.addResource(java.lang.String, java.lang.ClassLoader)

setMappingLocations

public void setMappingLocations(org.springframework.core.io.Resource[] mappingLocations)
Set locations of Hibernate mapping files, for example as classpath resource "classpath:example.hbm.xml". Supports any resource location via Spring's resource abstraction, for example relative paths like "WEB-INF/mappings/example.hbm.xml" when running in an application context.

Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

See Also:
Configuration.addInputStream(java.io.InputStream)

setCacheableMappingLocations

public void setCacheableMappingLocations(org.springframework.core.io.Resource[] cacheableMappingLocations)
Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml". Supports any resource location via Spring's resource abstraction, as long as the resource can be resolved in the file system.

Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

See Also:
Configuration.addCacheableFile(java.io.File)

setMappingJarLocations

public void setMappingJarLocations(org.springframework.core.io.Resource[] mappingJarLocations)
Set locations of jar files that contain Hibernate mapping resources, like "WEB-INF/lib/example.hbm.jar".

Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

See Also:
Configuration.addJar(java.io.File)

setMappingDirectoryLocations

public void setMappingDirectoryLocations(org.springframework.core.io.Resource[] mappingDirectoryLocations)
Set locations of directories that contain Hibernate mapping resources, like "WEB-INF/mappings".

Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

See Also:
Configuration.addDirectory(java.io.File)

setEntityInterceptor

public void setEntityInterceptor(org.hibernate.Interceptor entityInterceptor)
Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Will get applied to any new Session created by this factory.

See Also:
Configuration.setInterceptor(org.hibernate.Interceptor)

setNamingStrategy

public void setNamingStrategy(org.hibernate.cfg.NamingStrategy namingStrategy)
Set a Hibernate NamingStrategy for the SessionFactory, determining the physical column and table names given the info in the mapping document.

See Also:
Configuration.setNamingStrategy(org.hibernate.cfg.NamingStrategy)

setHibernateProperties

public void setHibernateProperties(java.util.Properties hibernateProperties)
Set Hibernate properties, such as "hibernate.dialect".

Note: Do not specify a transaction provider here when using Spring-driven transactions. It is also advisable to omit connection provider settings and use a Spring-set DataSource instead.

See Also:
setDataSource(javax.sql.DataSource)

getHibernateProperties

public java.util.Properties getHibernateProperties()
Return the Hibernate properties, if any. Mainly available for configuration through property paths that specify individual keys.


setAnnotatedClasses

public void setAnnotatedClasses(java.lang.Class<?>[] annotatedClasses)
Specify annotated entity classes to register with this Hibernate SessionFactory.

See Also:
org.hibernate.cfg.Configuration#addAnnotatedClass(Class)

setAnnotatedPackages

public void setAnnotatedPackages(java.lang.String[] annotatedPackages)
Specify the names of annotated packages, for which package-level annotation metadata will be read.

See Also:
org.hibernate.cfg.Configuration#addPackage(String)

setPackagesToScan

public void setPackagesToScan(java.lang.String... packagesToScan)
Specify packages to search for autodetection of your entity classes in the classpath. This is analogous to Spring's component-scan feature (org.springframework.context.annotation.ClassPathBeanDefinitionScanner).


setResourceLoader

public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Specified by:
setResourceLoader in interface org.springframework.context.ResourceLoaderAware

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.io.IOException
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.io.IOException

buildSessionFactory

protected org.hibernate.SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb)
Subclasses can override this method to perform custom initialization of the SessionFactory instance, creating it via the given Configuration object that got prepared by this LocalSessionFactoryBean.

The default implementation invokes LocalSessionFactoryBuilder's buildSessionFactory. A custom implementation could prepare the instance in a specific way (e.g. applying a custom ServiceRegistry) or use a custom SessionFactoryImpl subclass.

Parameters:
sfb - LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBean
Returns:
the SessionFactory instance
See Also:
LocalSessionFactoryBuilder.buildSessionFactory()

getConfiguration

public final org.hibernate.cfg.Configuration getConfiguration()
Return the Hibernate Configuration object used to build the SessionFactory. Allows for access to configuration metadata stored there (rarely needed).

Throws:
java.lang.IllegalStateException - if the Configuration object has not been initialized yet

getObject

public org.hibernate.SessionFactory getObject()
Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

getObjectType

public java.lang.Class<?> getObjectType()
Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

destroy

public void destroy()
Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean

spring-orm