Note some changes in the development of Hibernate4)

Source: Internet
Author: User

Note the development changes of Hibernate4.

Original article: http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861

 

 

--------------------------------------------------------------------

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 ....

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>

 

 

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.

 

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>

 

 

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 (); you can use the injected sessionFactory in basedao to obtain the session.

Note that the parent class baseServiceImpl must be matched during transaction configuration. Otherwise, an error occurs: it is not required before.

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.

 

Spring @ Transactional declarative transaction management, "currentSession" is defined as the Session currently managed by the Spring transaction manager. In this case, configure:

Org. springframework. orm. hibernate4.SpringSessionContext.

 

.

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.