From: http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861
The major changes to hibernate4 are only supported by spring3.1 and later versions. spring3.1 canceled the hibernatetemplate because the transaction management of hibernate4 is good and no spring extension is needed. Here, we briefly introduce the errors in the configuration of hibernate4 compared with hibernate3. We only list the problems and solutions. If you are interested in the Principles, please search for them by yourself.
Spring3.1 removed the hibernatedaosupport class. Hibernate4 needs to get the session through getcurrentsession. And set
<Prop key = "hibernate. current_session_context_class"> org. springframework. Orm. hibernate4.springsessioncontext </prop>
(Thread and JTA are used in hibernate3 ).
Set the cache to <prop key = "hibernate. cache. provider_class"> net. SF. ehcache. hibernate. ehcacheprovider </prop>.
<Prop key = "hibernate. cache. region. factory_class"> org. hibernate. cache. ehcache. ehcacheregionfactory </prop>
Spring manages hibernate transactions. Both the annotation and configuration file methods are changed:
<Bean id = "txmanager" class = "org. springframework. Orm. hibernate4.hibernatetransactionmanager">
<Property name = "sessionfactory"> <ref bean = "sessionfactory"/>
</Property>
</Bean>
The getcurrentsession () transaction is automatically closed, so the session is closed for querying data on some JSP pages. To query a database in JSP, you must add:
Org. springframework. Orm. hibernate4.support. opensessioninviewfilter filter.
When a resultset may only be accessed in a forward ction appears on the hibernate page, you need to set the hibernate result set to scroll.
<Prop key = "JDBC. use_scrollable_resultset"> false </prop>
--------------------------------------------------------------------
Find a good article, and I can find all the problems I encountered before. In fact, the key to these problems is the change in session management between hibernate4 and hibernate3.
Spring also makes corresponding changes ....
Error 1: Java. Lang. noclassdeffounderror: ORG/hibernate/Cache/cacheprovider
Cause: Spring's sessionfactory and transactionmanager are different from those that support hibernate3.
Solution:
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate4.localsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
...
</Bean>
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate4.hibernatetransactionmanager">
<Property name = "sessionfactory" ref = "sessionfactory"/>
</Bean>
Error 2: Java. Lang. nosuchmethoderror: org. hibernate. sessionfactory. opensession () lorg/hibernate/classic/session
Cause: After hibernate4, spring31 removed hibernatedaosupport, including data access without hibernatetemplate. This means that Dao needs to be rewritten and the session and Query Interfaces of Hibernate are used directly.
Solution: it took a whole day to rewrite Dao and perform unit tests on interfaces. This is one of the main reasons for the problem.
Error 3: Nested exception is org. hibernate. hibernateexception: No session found for current thread
Cause: some beans cannot obtain the current session. You need to upgrade the transactions of previous methods from not_support to required, readonly = true.
See https://jira.springsource.org/browse/SPR-9020, http://www.iteye.com/topic/1120924
Solution:
<TX: Advice id = "baseserviceadvice" transaction-Manager = "transactionmanager">
<TX: Attributes>
<TX: method name = "get *" Read-Only = "true" propagation = "required"/> <! -- Not_support -->
<TX: method name = "find *" Read-Only = "true" propagation = "required"/> <! -- Not_support -->
<TX: method name = "Save *" propagation = "required"/>
<TX: method name = "Update *" propagation = "required"/>
<TX: method name = "Remove *" propagation = "required"/>
<TX: method name = "add *" propagation = "required"/>
<! -- Other methods are required by default -->
<TX: method name = "*"/>
</TX: Attributes>
</TX: Advice>
Error 4: similar to error 3, java. Lang. nosuchmethoderror: org. hibernate. sessionfactory. opensession () lorg/hibernate/classic/session;
At org. springframework. Orm. hibernate3.sessionfactoryutils. dogetsession (sessionfactoryutils. Java: 324) [spring-orm-3.1.1.RELEASE.jar: 3.1.1.release]
At org. springframework. Orm. hibernate3.sessionfactoryutils. getsession (sessionfactoryutils. Java: 202) [spring-orm-3.1.1.RELEASE.jar: 3.1.1.release]
At org. springframework. Orm. hibernate3.support. opensessioninviewfilter
Cause: Due to the opensessioninview filter issue, if your configuration is still hibernate3, you need to change it to hibernate4.
<Filter>
<Filter-Name> opensessioninviewfilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate4.support. opensessioninviewfilter </filter-class>
</Filter>
--------------------------------------------------------------------
Because hibernate4 can fully implement transactions and conflicts with hibernatedao and hibernatetemplete in spring3.1, hibernatedaosupport and hibernatetemplete are no longer provided in spring3.1. You can only use the original hibernate method to use session:
Session session = sessionfactory. opensession ();
Session session = sessionfactory. getcurrentsession ();
In basedao, you can use the injected sessionfactory to get the session.
Note: the parent class baseserviceimpl must be matched during transaction configuration. Otherwise, an error occurs: No session found for currentthread, which was previously unnecessary.
The background Implementation of sessionfactory. getcurrentsession () is pluggable. Therefore, a New Extended Interface (Org. hibernate. Context. SPI. currentsessioncontext) And
The new configuration parameter (hibernate. current_session_context_class) is used to plug in the scope and context definitions of the current session.
InSpring @ transactional declarative transaction management, "currentsession" is defined as the session currently managed by the Spring transaction manager. In this case, configure:
Hibernate. current_session_context_class =Org. springframework. Orm. hibernate4.springsessioncontext.
Here, you must pay attention to the use of hibernate4. When you use getcurrentsession () instead of opensessioninview mode, the following problem occurs: When a method of list Propagation Behavior is supports, when you use another method of getpage () (no transaction) Org. hibernate. hibernateexception: No session found for current thread exception. This is because getcurrentsession () will not be automatically created without a session. I wonder if this is a bug implemented by spring3.1. Therefore, the best solution is to use the Propagation Behavior of required..