类 LocalSessionFactoryBean
- java.lang.Object
- org.springframework.orm.hibernate4.HibernateExceptionTranslator
- org.springframework.orm.hibernate4.LocalSessionFactoryBean
- 所有已实现的接口:
Aware,DisposableBean,FactoryBean<SessionFactory>,InitializingBean,ResourceLoaderAware,PersistenceExceptionTranslator
public class LocalSessionFactoryBean extends HibernateExceptionTranslator implements FactoryBean<SessionFactory>, ResourceLoaderAware, InitializingBean, DisposableBean
FactoryBeanthat creates a HibernateSessionFactory. 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.This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher. As of Spring 4.0, it is compatible with (the quite refactored) Hibernate 4.3 as well. We recommend using the latest Hibernate 4.2.x or 4.3.x version, depending on whether you need to remain JPA 2.0 compatible at runtime (Hibernate 4.2) or can upgrade to JPA 2.1 (Hibernate 4.3).
This class is similar in role to the same-named class in the
orm.hibernate3package. However, in practice, it is closer toAnnotationSessionFactoryBeansince its core purpose is to bootstrap aSessionFactoryfrom package scanning.NOTE: To set up Hibernate 4 for Spring-driven JTA transactions, make sure to either specify the
"jtaTransactionManager"bean property or to set the "hibernate.transaction.factory_class" property toCMTTransactionFactory. Otherwise, Hibernate's smart flushing mechanism won't work properly.- 从以下版本开始:
- 3.1
- 作者:
- Juergen Hoeller
- 另请参阅:
setDataSource(javax.sql.DataSource),setPackagesToScan(java.lang.String...),LocalSessionFactoryBuilder
构造器概要
构造器 构造器 说明 LocalSessionFactoryBean()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidafterPropertiesSet()Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected SessionFactorybuildSessionFactory(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.voiddestroy()Invoked by the containingBeanFactoryon destruction of a bean.ConfigurationgetConfiguration()Return the Hibernate Configuration object used to build the SessionFactory.PropertiesgetHibernateProperties()Return the Hibernate properties, if any.SessionFactorygetObject()Return an instance (possibly shared or independent) of the object managed by this factory.Class<?>getObjectType()Return the type of object that this FactoryBean creates, ornullif not known in advance.booleanisSingleton()Is the object managed by this factory a singleton?voidsetAnnotatedClasses(Class<?>... annotatedClasses)Specify annotated entity classes to register with this Hibernate SessionFactory.voidsetAnnotatedPackages(String... annotatedPackages)Specify the names of annotated packages, for which package-level annotation metadata will be read.voidsetCacheableMappingLocations(Resource... cacheableMappingLocations)Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml".voidsetCacheRegionFactory(RegionFactory cacheRegionFactory)Set the Hibernate RegionFactory to use for the SessionFactory.voidsetConfigLocation(Resource configLocation)Set the location of a single Hibernate XML config file, for example as classpath resource "classpath:hibernate.cfg.xml".voidsetConfigLocations(Resource... configLocations)Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".voidsetCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver)Set a Hibernate 4.1/4.2/4.3CurrentTenantIdentifierResolverto be passed on to the SessionFactory: as an instance, a Class, or a String class name.voidsetDataSource(DataSource dataSource)Set the DataSource to be used by the SessionFactory.voidsetEntityInterceptor(Interceptor entityInterceptor)Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.voidsetEntityTypeFilters(TypeFilter... entityTypeFilters)Specify custom type filters for Spring-based scanning for entity classes.voidsetHibernateProperties(Properties hibernateProperties)Set Hibernate properties, such as "hibernate.dialect".voidsetJtaTransactionManager(Object jtaTransactionManager)Set the SpringJtaTransactionManageror the JTATransactionManagerto be used with Hibernate, if any.voidsetMappingDirectoryLocations(Resource... mappingDirectoryLocations)Set locations of directories that contain Hibernate mapping resources, like "WEB-INF/mappings".voidsetMappingJarLocations(Resource... mappingJarLocations)Set locations of jar files that contain Hibernate mapping resources, like "WEB-INF/lib/example.hbm.jar".voidsetMappingLocations(Resource... mappingLocations)Set locations of Hibernate mapping files, for example as classpath resource "classpath:example.hbm.xml".voidsetMappingResources(String... mappingResources)Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml".voidsetMultiTenantConnectionProvider(Object multiTenantConnectionProvider)Set a Hibernate 4.1/4.2/4.3MultiTenantConnectionProviderto be passed on to the SessionFactory: as an instance, a Class, or a String class name.voidsetNamingStrategy(NamingStrategy namingStrategy)Set a Hibernate NamingStrategy for the SessionFactory, determining the physical column and table names given the info in the mapping document.voidsetPackagesToScan(String... packagesToScan)Specify packages to search for autodetection of your entity classes in the classpath.voidsetResourceLoader(ResourceLoader resourceLoader)Set the ResourceLoader that this object runs in.从类继承的方法 org.springframework.orm.hibernate4.HibernateExceptionTranslator
convertHibernateAccessException, translateExceptionIfPossible
构造器详细资料
LocalSessionFactoryBean
public LocalSessionFactoryBean()
方法详细资料
setDataSource
public void setDataSource(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(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.
setConfigLocations
public void setConfigLocations(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.
setMappingResources
public void setMappingResources(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.
setMappingLocations
public void setMappingLocations(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.
setCacheableMappingLocations
public void setCacheableMappingLocations(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.
setMappingJarLocations
public void setMappingJarLocations(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.
setMappingDirectoryLocations
public void setMappingDirectoryLocations(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.
setEntityInterceptor
public void setEntityInterceptor(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.
setNamingStrategy
public void setNamingStrategy(NamingStrategy namingStrategy)
Set a Hibernate NamingStrategy for the SessionFactory, determining the physical column and table names given the info in the mapping document.
setJtaTransactionManager
public void setJtaTransactionManager(Object jtaTransactionManager)
Set the SpringJtaTransactionManageror the JTATransactionManagerto be used with Hibernate, if any. Implicitly sets upJtaPlatformandCMTTransactionStrategy.
setMultiTenantConnectionProvider
public void setMultiTenantConnectionProvider(Object multiTenantConnectionProvider)
Set a Hibernate 4.1/4.2/4.3MultiTenantConnectionProviderto be passed on to the SessionFactory: as an instance, a Class, or a String class name.Note that the package location of the
MultiTenantConnectionProviderinterface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
setCurrentTenantIdentifierResolver
public void setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver)
Set a Hibernate 4.1/4.2/4.3CurrentTenantIdentifierResolverto be passed on to the SessionFactory: as an instance, a Class, or a String class name.
setCacheRegionFactory
public void setCacheRegionFactory(RegionFactory cacheRegionFactory)
Set the Hibernate RegionFactory to use for the SessionFactory. Allows for using a Spring-managed RegionFactory instance.Note: If this is set, the Hibernate settings should not define a cache provider to avoid meaningless double configuration.
setEntityTypeFilters
public void setEntityTypeFilters(TypeFilter... entityTypeFilters)
Specify custom type filters for Spring-based scanning for entity classes.Default is to search all specified packages for classes annotated with
@javax.persistence.Entity,@javax.persistence.Embeddableor@javax.persistence.MappedSuperclass.- 从以下版本开始:
- 4.1
- 另请参阅:
setPackagesToScan(java.lang.String...)
setHibernateProperties
public void setHibernateProperties(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.
getHibernateProperties
public Properties getHibernateProperties()
Return the Hibernate properties, if any. Mainly available for configuration through property paths that specify individual keys.
setAnnotatedClasses
public void setAnnotatedClasses(Class<?>... annotatedClasses)
Specify annotated entity classes to register with this Hibernate SessionFactory.
setAnnotatedPackages
public void setAnnotatedPackages(String... annotatedPackages)
Specify the names of annotated packages, for which package-level annotation metadata will be read.
setPackagesToScan
public void setPackagesToScan(String... packagesToScan)
Specify packages to search for autodetection of your entity classes in the classpath. This is analogous to Spring's component-scan feature (ClassPathBeanDefinitionScanner).
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
从接口复制的说明:ResourceLoaderAwareSet the ResourceLoader that this object runs in.This might be a ResourcePatternResolver, which can be checked through
instanceof ResourcePatternResolver. See also theResourcePatternUtils.getResourcePatternResolvermethod.Invoked after population of normal bean properties but before an init callback like InitializingBean's
afterPropertiesSetor a custom init-method. Invoked before ApplicationContextAware'ssetApplicationContext.- 指定者:
setResourceLoader在接口中ResourceLoaderAware- 参数:
resourceLoader- the ResourceLoader object to be used by this object- 另请参阅:
ResourcePatternResolver,ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)
afterPropertiesSet
public void afterPropertiesSet() throws IOException
从接口复制的说明:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet在接口中InitializingBean- 抛出:
IOException
buildSessionFactory
protected 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.
- 参数:
sfb- LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBean- 返回:
- the SessionFactory instance
- 另请参阅:
LocalSessionFactoryBuilder.buildSessionFactory()
getConfiguration
public final Configuration getConfiguration()
Return the Hibernate Configuration object used to build the SessionFactory. Allows for access to configuration metadata stored there (rarely needed).- 抛出:
IllegalStateException- if the Configuration object has not been initialized yet
getObject
public SessionFactory getObject()
从接口复制的说明:FactoryBeanReturn an instance (possibly shared or independent) of the object managed by this factory.As with a
BeanFactory, this allows support for both the Singleton and Prototype design pattern.If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding
FactoryBeanNotInitializedException.As of Spring 2.0, FactoryBeans are allowed to return
nullobjects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.- 指定者:
getObject在接口中FactoryBean<SessionFactory>- 返回:
- an instance of the bean (can be
null) - 另请参阅:
FactoryBeanNotInitializedException
getObjectType
public Class<?> getObjectType()
从接口复制的说明:FactoryBeanReturn the type of object that this FactoryBean creates, ornullif not known in advance.This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
nullhere. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.- 指定者:
getObjectType在接口中FactoryBean<SessionFactory>- 返回:
- the type of object that this FactoryBean creates, or
nullif not known at the time of the call - 另请参阅:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
从接口复制的说明:FactoryBeanIs the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from
getObject()might get cached by the owning BeanFactory. Hence, do not returntrueunless the FactoryBean always exposes the same reference.The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
falsedoes not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBeaninterface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()method. PlainFactoryBeanimplementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()implementation returnsfalse.- 指定者:
isSingleton在接口中FactoryBean<SessionFactory>- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject(),SmartFactoryBean.isPrototype()
destroy
public void destroy()
从接口复制的说明:DisposableBeanInvoked by the containingBeanFactoryon destruction of a bean.- 指定者:
destroy在接口中DisposableBean