
full-hibernate-plugin-for-struts2 - 3_Hibernate_Core_Sessions_Transactions_injecton.wiki
Introduction
This feature provides an Interceptor that open and sets in the value stack a Hibernate Core Session and closes (or not) this after the results are rendered for the client. The same Interceptor opens and sets in the value stack a Transaction and commit this after the results are rendered for the client if a rollback method was not invoked.
No code lines need to be added to manage Hibernate sessions in Struts 2 projects. Only some XML (or .properties) lines needed.
This feature follows the concepts in http://www.hibernate.org/43.html
Important! hibernatesession-plugin-for-struts2 users: You are strongly encouraged to migrate for this plugin!
<hr/>
Usage
At first, install the plugin.
If you want to use the Hibernate Core Sessions and Transactions injection capability, your(s) mapping package(s) need to extend the package hibernate-default. So, you have to use some configuration properties in struts.xml or struts.properties. This properties are:
- hibernatePlugin.sessionTarget: Comma-separated Hibernate Session Target in the Action ValueStack. This property can be used in 2 ways:
- Simple values. Examples: myHibSession, myHibSession2; hibSession; myDao.session, myDao2.session
- Values using Wildcard. Examples: **Dao.session;**Facade.hibernateSession
This property is Optional.
<hr/>
- hibernatePlugin.transactionTarget: Comma-separated Hibernate Transaction Target in the Action ValueStack. This property can be used in 2 ways:
- Simple values. Examples: myHibTransaction, myHibTransaction2; hibernateTransaction; myDao.transaction, myDao2.transaction
- Values using Wildcard. Examples: **Dao.transaction;**Facade.hibernateTransaction
This property is Optional.
<hr/>
- hibernatePlugin.configurationFiles: Comma-separated Hibernate Configuration file names. Note that if hibernatePlugin.customSessionFactoryClass property is used that is not guarantee about the using of the files named here.
This property is Optional. Default /hibernate.cfg.xml.
<hr/>
- hibernatePlugin.autoRebuild (version 2.0+): Define if the Full Hibernate Plugin's Session Factory will rebuild all configured SessionFactories when an org.hibernate.exception.JDBCConnectionException is thrown. Use true or false.
This property is Optional. Default true.
<hr/>
- hibernatePlugin.configurationType (Deprecated since 1.2): Hibernate Configuration Type. Values: xml or annotation.
Note that if hibernatePlugin.customSessionFactoryClass property is used that is not guarantee about the using of the configuration type named here.
This property is Optional. Default xml.
<hr/>
- hibernatePlugin.customSessionFactoryClass: Full qualified name of the custom class used as a Hibernate Session Factory.
This property is Optional. If not used, the Hibernate Plugin will use an internal Session Factory Class (com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory).
<hr/>
- hibernatePlugin.getSessionMethod: Method used to get the Hibernate Session from the Session Factory class configured at hibernatePlugin.customSessionFactoryClass.
Examples: getSession; getHibernateSession
Optional, but Required if hibernatePlugin.customSessionFactoryClass property is used.
<hr/>
- hibernatePlugin.rebuildSessionFactoryMethod: Public method from the Hibernate Session Factory class for rebuild configurations.
Optional, but Required if hibernatePlugin.customSessionFactoryClass property is used.
<hr/>
- hibernatePlugin.staticGetSessionMethod: Is the method used to get the Hibernate Session static? If true the method if used in static mode (ie. MySessionFactoryClass.getSession()).
If false the Hibernate Plugin will instantiate the Session Factory class.
Optional, but Required if hibernatePlugin.customSessionFactoryClass property is used.
Default true.
<hr/>
- hibernatePlugin.closeSessionMethod (version 1.1+): Method used to CLOSE the Hibernate Session from the Session Factory class configured at hibernatePlugin.customSessionFactoryClass.
Example: closeSession
Optional, but Required if hibernatePlugin.customSessionFactoryClass property is used.
<hr/>
hibernatePlugin.useSessionObjectInCloseSessionMethod (version 1.1+): Indicates the use of the Hibernate Session Object as parameter in close session method at the Session Factory class configured at hibernatePlugin.customSessionFactoryClass.
Use true or false
Optional.
Default true.hibernatePlugin.staticRebuildSessionFactoryMethod: Set the access mode of the rebuild Session Factory method of the Session Factory class configured at hibernatePlugin.customSessionFactoryClass.
If true the method if used in static mode (ie. MySessionFactoryClass.rebuildMySessionFactory()).
If false the Hibernate Plugin will instantiate the Session Factory class.
Optional, but Required if hibernatePlugin.customSessionFactoryClass property is used. Default true.
<hr/>
- hibernatePlugin.closeSessionAfterInvoke:If true, the Hibernate Session of the request will be closed after the response.
If false, the Hibernate Session of the request WILL NOT be closed after the response.
Optional. Default true.
<hr/>
- hibernatePlugin.searchMappingsInsideJars (version 2.1.1+):If true, @SessionTarget and @TransactionTarget will be searched in Jar files (performance will decrease). Use true only if you have one of these annotations in classes inside Jar files.
Optional. Default false.
<hr/>
Easiest configuration
If you do not use a custom class as a Hibernate Session Factory and don't want to disable auto close sessions function, you will need to use just 2 properties in your project:
hibernatePlugin.sessionTarget and hibernatePlugin.transactionTarget.
Look an example here.
Instead, you can use the Annotations configuration.
Annotations configuration
You can use annotations over your session and transaction objects:
@SessionTarget (for Hibernate Session objects)
@TransactionTarget (for Transaction objects)
This feature can be used with the hibernatePlugin.sessionTarget and hibernatePlugin.transactionTarget configuration properties. If both are used (annotations and properties), both will be used by the plugin.>
This annotations can be used in Actions or in any class in your project (DAOs, Facades...).
Look an example here.
<hr/>
More details about "hibernate-default" package
This plugin provides a mapping package called hibernate-default. And this has 3 interceptor stacks indicated for general use:
- basicStackHibernate: Like Struts2 basickStack (NO validations here!), but with Hibernate Core session and transaction injections capability.
- defaultStackHibernate: Like Struts2 defaultStack, but DO NOT USE Struts2 validation methods (annotation and XML). Uses Hibernate Validation framework instead.
- defaultStackHibernateStrutsValidation: Struts2 defaultStack + plugin basicStackHibernate.
This package extends the hibernate-default package, so all default Struts2 configurations can be used if you need.
hibernate-default package is abstract, so you can extends this with other. Example:
...
<package name="principal" extends="hibernate-default,jasperreports-default,json-default" >
...
<hr/>
The Hibernate Core Session Factory
This plugin provides a powerfull Hibernate Core Session Factory class.
The class is com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.
If you do not configure a custom Hibernate Core Session Factory class, you can use this methods:
* getSession(): Get the current thread's Hibernate Core Session
* closeSession(): Close the current thread's Hibernate Core Session
* getNewSession(): Get an "isolated" Hibernate Core Session
* rebuildSessionFactory(): Reload the session factory configurations according the configuration files
* destroyFactory(): Destroy Session Factory. In Web Applications with Hibernate, when the context is reload, it's necessary to destroy the session factory to evict problems with connection pools like C3P0.
* getSessionFactory(String sessionFactoryName): Returns one of the configured SessionFactories (version 2.0+)