org.codehaus.groovy.grails.orm.hibernate
Class ConfigurableLocalSessionFactoryBean

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

public class ConfigurableLocalSessionFactoryBean
extends org.springframework.orm.hibernate4.HibernateExceptionTranslator
implements org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>, org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanClassLoaderAware

A SessionFactory bean that allows the configuration class to be changed and customise for usage within Grails.

Since:
07-Jul-2005

Field Summary
protected  org.springframework.context.ApplicationContext applicationContext
           
protected  java.lang.ClassLoader classLoader
           
protected  java.lang.Class<?> configClass
           
protected  java.lang.Class<?> currentSessionContextClass
           
protected  java.lang.String dataSourceName
           
protected  java.util.Map<java.lang.String,java.lang.Object> eventListeners
           
protected  org.codehaus.groovy.grails.commons.GrailsApplication grailsApplication
           
protected  org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners hibernateEventListeners
           
protected  boolean proxyIfReloadEnabled
           
protected  java.lang.String sessionFactoryBeanName
           
 
Constructor Summary
ConfigurableLocalSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  void buildSessionFactory()
           
protected  void buildSessionFactoryProxy()
           
 void destroy()
           
protected  org.hibernate.SessionFactory doBuildSessionFactory()
           
 java.lang.Class<?>[] getAnnotatedClasses()
           
 java.lang.String[] getAnnotatedPackages()
           
 org.springframework.core.io.Resource[] getCacheableMappingLocations()
           
protected  java.lang.String getCauseMessage(org.hibernate.HibernateException e)
           
 java.lang.Class<?> getConfigClass()
           
 org.springframework.core.io.Resource[] getConfigLocations()
           
 org.hibernate.cfg.Configuration getConfiguration()
          Return the Hibernate Configuration object used to build the SessionFactory.
 java.lang.Class<?> getCurrentSessionContextClass()
           
 javax.sql.DataSource getDataSource()
           
 java.lang.String getDataSourceName()
           
 org.hibernate.Interceptor getEntityInterceptor()
           
 java.util.Map<java.lang.String,java.lang.Object> getEventListeners()
           
 org.codehaus.groovy.grails.commons.GrailsApplication getGrailsApplication()
           
 org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners getHibernateEventListeners()
           
 java.util.Properties getHibernateProperties()
          Return the Hibernate properties, if any.
 org.springframework.core.io.Resource[] getMappingDirectoryLocations()
           
 org.springframework.core.io.Resource[] getMappingJarLocations()
           
 org.springframework.core.io.Resource[] getMappingLocations()
           
 java.lang.String[] getMappingResources()
           
 org.hibernate.cfg.NamingStrategy getNamingStrategy()
           
 org.hibernate.SessionFactory getObject()
           
 java.lang.Class<?> getObjectType()
           
 java.lang.String[] getPackagesToScan()
           
 java.lang.String getSessionFactoryBeanName()
           
protected  boolean isCacheConfigurationError(java.lang.Throwable cause)
           
 boolean isProxyIfReloadEnabled()
           
 boolean isSingleton()
           
protected  GrailsAnnotationConfiguration newConfiguration()
           
 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 setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
           
 void setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
           
 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 setConfigClass(java.lang.Class<?> configClass)
          Sets the class to be used for Hibernate Configuration.
 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 setCurrentSessionContextClass(java.lang.Class<?> currentSessionContextClass)
          Sets class to be used for the Hibernate CurrentSessionContext.
 void setDataSource(javax.sql.DataSource dataSource)
          Set the DataSource to be used by the SessionFactory.
 void setDataSourceName(java.lang.String name)
           
 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 setEventListeners(java.util.Map<java.lang.String,java.lang.Object> eventListeners)
          Specify the Hibernate event listeners to register, with listener types as keys and listener objects as values.
 void setGrailsApplication(org.codehaus.groovy.grails.commons.GrailsApplication grailsApplication)
           
 void setHibernateEventListeners(org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners listeners)
           
 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 setProxyIfReloadEnabled(boolean proxyIfReloadEnabled)
           
 void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
           
 void setSessionFactoryBeanName(java.lang.String name)
           
 
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
 

Field Detail

grailsApplication

protected org.codehaus.groovy.grails.commons.GrailsApplication grailsApplication

classLoader

protected java.lang.ClassLoader classLoader

configClass

protected java.lang.Class<?> configClass

currentSessionContextClass

protected java.lang.Class<?> currentSessionContextClass

eventListeners

protected java.util.Map<java.lang.String,java.lang.Object> eventListeners

hibernateEventListeners

protected org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners hibernateEventListeners

applicationContext

protected org.springframework.context.ApplicationContext applicationContext

proxyIfReloadEnabled

protected boolean proxyIfReloadEnabled

sessionFactoryBeanName

protected java.lang.String sessionFactoryBeanName

dataSourceName

protected java.lang.String dataSourceName
Constructor Detail

ConfigurableLocalSessionFactoryBean

public ConfigurableLocalSessionFactoryBean()
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.


getDataSource

public javax.sql.DataSource getDataSource()

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)

getConfigLocations

public org.springframework.core.io.Resource[] getConfigLocations()

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)

getMappingResources

public java.lang.String[] getMappingResources()

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)

getMappingLocations

public org.springframework.core.io.Resource[] getMappingLocations()

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)

getCacheableMappingLocations

public org.springframework.core.io.Resource[] getCacheableMappingLocations()

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)

getMappingJarLocations

public org.springframework.core.io.Resource[] getMappingJarLocations()

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)

getMappingDirectoryLocations

public org.springframework.core.io.Resource[] getMappingDirectoryLocations()

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)

getEntityInterceptor

public org.hibernate.Interceptor getEntityInterceptor()

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)

getNamingStrategy

public org.hibernate.cfg.NamingStrategy getNamingStrategy()

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:
Configuration.addAnnotatedClass(Class)

getAnnotatedClasses

public java.lang.Class<?>[] getAnnotatedClasses()

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:
Configuration.addPackage(String)

getAnnotatedPackages

public java.lang.String[] getAnnotatedPackages()

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 (ClassPathBeanDefinitionScanner).


getPackagesToScan

public java.lang.String[] getPackagesToScan()

setResourceLoader

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

setProxyIfReloadEnabled

public void setProxyIfReloadEnabled(boolean proxyIfReloadEnabled)
Parameters:
proxyIfReloadEnabled - Sets whether a proxy should be created if reload is enabled

isProxyIfReloadEnabled

public boolean isProxyIfReloadEnabled()

setCurrentSessionContextClass

public void setCurrentSessionContextClass(java.lang.Class<?> currentSessionContextClass)
Sets class to be used for the Hibernate CurrentSessionContext.

Parameters:
currentSessionContextClass - An implementation of the CurrentSessionContext interface

getCurrentSessionContextClass

public java.lang.Class<?> getCurrentSessionContextClass()

setConfigClass

public void setConfigClass(java.lang.Class<?> configClass)
Sets the class to be used for Hibernate Configuration.

Parameters:
configClass - A subclass of the Hibernate Configuration class

getConfigClass

public java.lang.Class<?> getConfigClass()

setGrailsApplication

public void setGrailsApplication(org.codehaus.groovy.grails.commons.GrailsApplication grailsApplication)
Parameters:
grailsApplication - The grailsApplication to set.

getGrailsApplication

public org.codehaus.groovy.grails.commons.GrailsApplication getGrailsApplication()

setHibernateEventListeners

public void setHibernateEventListeners(org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners listeners)

getHibernateEventListeners

public org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners getHibernateEventListeners()

setSessionFactoryBeanName

public void setSessionFactoryBeanName(java.lang.String name)

getSessionFactoryBeanName

public java.lang.String getSessionFactoryBeanName()

setDataSourceName

public void setDataSourceName(java.lang.String name)

getDataSourceName

public java.lang.String getDataSourceName()

setEventListeners

public void setEventListeners(java.util.Map<java.lang.String,java.lang.Object> eventListeners)
Specify the Hibernate event listeners to register, with listener types as keys and listener objects as values. Instead of a single listener object, you can also pass in a list or set of listeners objects as value.

See the Hibernate documentation for further details on listener types and associated listener interfaces.

Parameters:
eventListeners - Map with listener type Strings as keys and listener objects as values

getEventListeners

public java.util.Map<java.lang.String,java.lang.Object> getEventListeners()

afterPropertiesSet

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

buildSessionFactory

protected void buildSessionFactory()
                            throws java.lang.Exception
Throws:
java.lang.Exception

doBuildSessionFactory

protected org.hibernate.SessionFactory doBuildSessionFactory()

buildSessionFactoryProxy

protected void buildSessionFactoryProxy()

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

newConfiguration

protected GrailsAnnotationConfiguration newConfiguration()
                                                  throws java.lang.Exception
Throws:
java.lang.Exception

setBeanClassLoader

public void setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
Specified by:
setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware

getCauseMessage

protected java.lang.String getCauseMessage(org.hibernate.HibernateException e)

isCacheConfigurationError

protected boolean isCacheConfigurationError(java.lang.Throwable cause)

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException