Go to upgrade hibernate>4,spring>3.1 notes

Source: Internet
Author: User

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
  1. <filter >
  2. <filter-name>opensessioninviewfilter </ Filter-name >
  3. <filter-class > Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class >
  4. <init-param >
  5. <param-name>singlesession</Param-nam E >
  6. <param-value>true</param-value>   
  7. </init-param >
  8. <init-param >
  9. <param-name>sessionfactorybeanname </ Param-name >
  10. <param-value>mysessionfactory</param-value c7>>
  11. </init-param >
  12. </filter >
  13.       
  14. <filter-mapping >
  15. <filter-name>opensessioninviewfilter </ Filter-name >
  16. <url-pattern>/*</url-pattern >   
  17. </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.