The difference between Hibernate3 and version 4

Source: Internet
Author: User
Ibernate4 changes are larger than the spring3.1 version can support, Spring3.1 canceled Hibernatetemplate, because the HIBERNATE4 transaction management is very good, no spring to expand. Here is a brief introduction of hibernate4 relative to the Hibernate3 configuration errors, only listed the problems and solutions, detailed principles if you are interested or go to their own search, a lot of online.

1, Spring3.1 removed the Hibernatedaosupport class. Hibernate4 need to get the session through Getcurrentsession (). and set
<prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate4.springsessioncontext</prop>
(at the time of Hibernate3, thread and JTA).

2, cache settings changed 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>

3, spring to hibernate transaction management, whether the annotation method or the configuration file method unification change to:
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "sessionfactory" ><ref bean= "Sessionfactory"/>
</property>
</bean>

4, Getcurrentsession () transaction will automatically shut down, so in some JSP page query data will be closed session. To query the database in JSP you need to add:
Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter filter.

Hibernate paging appears ResultSet May is accessed in a forward direction need to set hibernate result set scrolling
<prop key= "Jdbc.use_scrollable_resultset" >false</prop>


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

find a good article, I have encountered a problem in this can be found. In fact, the key to these problems is hibernate4 and hibernate3 the change of Session management.

spring also makes the corresponding changes ....

Error 1:java.lang.noclassdeffounderror:org/hibernate/cache/cacheprovider

reason: Spring's sessionfactory and TransactionManager are different from support hibernate3.

Solve:

 

<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 hibernatedaosupport removal, including data access, does not require hibernatetemplate, which means that DAO needs to be rewritten, Use the Hibernate session and query interface directly.

solution: In order to rewrite DAO, took a full day, and then each interface for unit testing, which is a major cause of egg pain.


Error 3:nested exception is org.hibernate.HibernateException:No session found to current thread

cause: Some beans were found unable to get the current session, and the transaction of previous methods needed to be upgraded from Not_support to Required,readonly=true

See https://jira.springsource.org/browse/SPR-9020, http://www.iteye.com/topic/1120924

Solve:

 

<tx:advice id= "Baseserviceadvice" transaction-manager= "TransactionManager" >

<tx:attributes>

<tx:method name= "get*" read-only= "true" propagation= " REQUIRED"/><!--before not_support-->

<tx:method name= "find*" read-only= "true" propagation= " REQUIRED"/><!--before 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"/>

<!--default other methods are required-->

<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


reason: Because of the problem of Opensessioninview filter, if your configuration is still hibernate3, you need to change to Hibernate4

<filter>

<filter-name>openSessionInViewFilter</filter-name>

<filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class >

</filter>


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

since Hibernate4 has been able to achieve business, and Spring3.1 in the Hibernatedao,hibernatetemplete, and so there is conflict, So Spring3.1 has not provided hibernatedaosupport,hibernatetemplete, only in hibernate original way with session:

Session session = Sessionfactory.opensession ();
Session session = Sessionfactory.getcurrentsession ();
In Basedao, you can use the injected sessionfactory to get the session.

Note that when you configure a transaction, you must also match the parent class Baseserviceimpl, or you will receive an error: no session found for CurrentThread, which was not needed before

the background implementation of the Sessionfactory.getcurrentsession () is pluggable. Therefore, a new extension interface (Org.hibernate.context.spi.CurrentSessionContext) is introduced and

The new configuration parameter (Hibernate.current_session_context_class) to plug the definition of what is the scope and context of the current session (scope and contexts).


in spring @Transactional declarative transaction management, "Currentsession" is defined as the session currently managed by the Spring transaction manager, which should be configured at this time:

Hibernate.current_session_context_class=org.springframework.orm.hibernate4.springsessioncontext.


It is important to note here that using HIBERNATE4, when using the Getcurrentsession () when not using Opensessioninview mode, has the following problem: when there is a method list propagation behavior is supports, The org.hibernate.HibernateException:No session found for the current thread exception is thrown when the list method is called by another method GetPage () (no transaction). This is because getcurrentsession () does not automatically create one without a session, and does not know if this is a Spring3.1 implementation bug. So the best solution is to use required propagation behavior.


There are too many tags to go from http://hi.baidu.com/austincao/item/fc9907da3d854e44fa576861

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.