| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
FactoryBean that creates a local Hibernate SessionFactory instance. Behaves like a SessionFactory instance when used as bean reference, e.g. for HibernateTemplate's "sessionFactory" property. Note that switching to JndiObjectFactoryBean is just a matter of configuration!
The typical usage will be to register this as singleton factory (for a certain underlying JDBC DataSource) in an application context, and give bean references to application services that need it.
Configuration settings can either be read from a Hibernate XML file, specified as "configLocation", or completely via this class. A typical local configuration consists of one or more "mappingResources", various "hibernateProperties" (not strictly necessary), and a "dataSource" that the SessionFactory should use. The latter can also be specified via Hibernate properties, but "dataSource" supports any Spring-configured DataSource, instead of relying on Hibernate's own connection providers.
This SessionFactory handling strategy is appropriate for most types of applications, from Hibernate-only single database apps to ones that need distributed transactions. Either HibernateTransactionManager or JtaTransactionManager can be used for transaction demarcation, the latter only being necessary for transactions that span multiple databases.
Registering a SessionFactory with JNDI is only advisable when using Hibernate's JCA Connector, i.e. when the application server cares for initialization. Else, portability is rather limited: Manual JNDI binding isn't supported by some application servers (e.g. Tomcat). Unfortunately, JCA has drawbacks too: Its setup is container-specific and can be tedious.
Note that the JCA Connector's sole major strength is its seamless cooperation with EJB containers and JTA services. If you do not use EJB and initiate your JTA transactions via Spring's JtaTransactionManager, you can get all benefits including distributed transactions and proper transactional JVM-level caching with local SessionFactory setup too - without any configuration hassle like container-specific setup.
Note: Spring's Hibernate support requires Hibernate 2.x (2.1 recommended).
Method Summary | |
void | afterPropertiesSet() Initialize the SessionFactory for the given or the default location. |
void | destroy() Close the SessionFactory on bean factory shutdown. |
static LobHandler | getConfigTimeLobHandler() Return the LobHandler for the currently configured Hibernate SessionFactory, to be used by Type implementations like ClobStringType. |
Object | getObject() Return the singleton SessionFactory. |
Class | getObjectType() |
boolean | isSingleton() |
void | setConfigLocation(String configLocation) Set the location of the Hibernate XML config file as class path resource. |
void | setDataSource(DataSource dataSource) Set the DataSource to be used by the SessionFactory. |
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. |
void | setHibernateProperties(Properties hibernateProperties) Set Hibernate properties, like "hibernate.dialect". |
void | setJtaTransactionManager(TransactionManager jtaTransactionManager) Set the JTA TransactionManager to be used for Hibernate's TransactionManagerLookup. |
void | setLobHandler(LobHandler lobHandler) Set the LobHandler to be used by the SessionFactory. |
void | setMappingResourceJars(String[] mappingResourceJars) Set jar files in the class path that contain Hibernate mapping resources, like "example.hbm.jar". |
void | setMappingResources(String[] mappingResources) Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml". |
void | setSchemaUpdate(boolean schemaUpdate) Set whether to execute a schema update after SessionFactory initialization. |
Method Detail |
public void afterPropertiesSet()
throws java.lang.IllegalArgumentException,
HibernateException
public void destroy()
throws HibernateException
public static LobHandler getConfigTimeLobHandler()
This instance will be set before initialization of the corresponding SessionFactory, and reset immediately afterwards. It is thus only available in constructors of UserType implementations.
public Object getObject()
public Class getObjectType()
public boolean isSingleton()
public void setConfigLocation(String configLocation)
Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.
public void setDataSource(DataSource dataSource)
Note: If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.
public void setEntityInterceptor(Interceptor entityInterceptor)
Such an interceptor can either be set at the SessionFactory level, i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager. It's preferable to set it on LocalSessionFactoryBean or HibernateTransactionManager to avoid repeated configuration and guarantee consistent behavior in transactions.
public void setHibernateProperties(Properties hibernateProperties)
Can be used to override values in a Hibernate XML config file, or to specify all necessary properties locally.
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.
public void setJtaTransactionManager(TransactionManager jtaTransactionManager)
Note: If this is set, the Hibernate settings should not define a transaction manager lookup to avoid meaningless double configuration.
public void setLobHandler(LobHandler lobHandler)
public void setMappingResourceJars(String[] mappingResourceJars)
Can be used to override values from a Hibernate XML config file, or to specify all mappings locally.
public void setMappingResources(String[] mappingResources)
Can be used to override values from a Hibernate XML config file, or to specify all mappings locally.
public void setSchemaUpdate(boolean schemaUpdate)
For details on how to make schema update scripts work, see the Hibernate documentation, as this class leverages the same schema update script support in net.sf.hibernate.cfg.Configuration as Hibernate's own SchemaUpdate tool.
Association Links |
to Class java.lang.String
to Class java.lang.String
to Class java.lang.String
to Class java.util.Properties
to Class javax.sql.DataSource
to Class javax.transaction.TransactionManager
to Class org.springframework.jdbc.support.lob.LobHandler
| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |