It's been a long time since I looked at Hibernate and spring's release version. I want to upgrade my heart. The problem really came to a lot ... Record them.
Spring to 3.1 is simple, delete the old package, add a new package, and then configure the Xsi:schemalocation version from 3.0 to 3.1 other basic can be run intact.
The hibernate (using annotation) bean configuration in spring is then changed.
First Sessionfactory's Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean. To change into Org.springframework.orm.hibernate4.LocalSessionFactoryBean thought as long as the hibernate3 replaced with 4, and found that spring There is no such class in 3.1. Annotation is also used by Localsessionfactorybean. This does not change the report cache.xxx cannot find the class error.
Then change the Org.springframework.orm.hibernate3.HibernateTransactionManager into Org.springframework.orm.hibernate4.HibernateTransactionManager
Then remove the hibernatetemplate. This is important, Spring 3.1 no longer supports hibernatetemplate. I don't know what the reason is. I didn't dig.
Then there is a large area of the modified class ...
Because no longer use hibernatetemplate, will change to sessionfactory.getcurrentsession () ...
This transformation is not really difficult, the only difference is the operation of the Detachedcriteria.
Hibernatetemplate.findbycriteria (Detachedcriteria,first,max) replaced by Detachedcriteria.getexecutablecriteria ( Sessionfactory.getcurrentsession ()). Setfirstresult (first) setmaxresults (max). List ()
It is easiest to use sessionfactory.getcurrentsession (). Createcriteria (Xxx.class) instead of Detachedcriteria.
Criteria to see Hibernate's official help There is a very detailed explanation, enough to easily replace the hibernatetemplate, the completion of the basic upgrade is completed.
MVC Opensessioninviewfilter:
[HTML]View Plain Copy
- <filter >
- <filter-name>opensessioninviewfilter </ Filter-name >
- <filter-class > Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class >
- <init-param >
- <param-name>singlesession</Param-nam E >
- <param-value>true</param-value>
- </init-param >
- <init-param >
- <param-name>sessionfactorybeanname </ Param-name >
- <param-value>mysessionfactory</param-value c7>>
- </init-param >
- </filter >
-
- <filter-mapping >
- <filter-name>opensessioninviewfilter </ Filter-name >
- <url-pattern>/*</url-pattern >
- </filter-mapping >
Another problem with multiple data sources:
Original old version does not have the problem ... Is that there are 2 data sources, the original definition of the 2 sessionfactory can be used normally, but after the upgrade, the second appears No Session found for the current thread problem.
This problem can be solved simply by using the following method. Insurance or use JTA to achieve better ...
The spring configuration defines 2 TransactionManager, and then Tx:annotation-driven uses the one named TransactionManager by default.
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "SessionFactory1"/>
</bean>
<bean id= "TransactionManager2" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "SessionFactory2"/>
</bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>
Then add @Transactional (value= "TransactionManager2") with the second Sessionfactory method in the class.
@Transactional
public void Method1 () {
METHOD2 ()
}
@Transactional (value= "TransactionManager2")
public void Method2 () {
SessionFactory2 is used here.
}
Because my second data source is read-only, I don't know if the transaction will rollback properly. Theoretically, it won't roll back ...
==================================================================
With multiple data sources:
Http://www.360doc.com/content/09/1222/17/18042_11742680.shtml
Http://www.cnblogs.com/sky7034/archive/2011/08/11/2134411.html
http://blog.csdn.net/wangdonghua2261/article/details/3872975
Go to upgrade hibernate>4,spring>3.1 notes