Notice some changes in the development of HIBERNATE4

Source: Internet
Author: User

Hibernate4 change is larger than spring3.1 version can support, Spring3.1 canceled the hibernatetemplate because Hibernate4 's transaction management was fine, without spring expanding the . Here is a brief introduction of hibernate4 relative to the Hibernate3 configuration error, only listed the problem and the solution, detailed principles if you are interested or go to their own search, online a lot.

  1. Spring3.1 removed the Hibernatedaosupport class. Hibernate4 need to get session through Getcurrentsession (). and set
    <prop key= " Hibernate.current_session_context_class ">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    (Thread and JTA in Hibernate3).

  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's transaction management of hibernate, either annotation or configuration file, is changed to:
    <bean id= "Txmanager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager ">   
    <property name= "sessionfactory" ><ref bean= "Sessionfactory"/>
    </property>  
    </bean>

  4. the Getcurrentsession () transaction is automatically closed, so querying the data on a JSP page will close the session. to Query the database in JSP need to join:
    Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter filter.

  5. Hibernate page appears ResultSet May is accessed in a forward direction Hibernate result set scrolling needs to be set
    <prop key= " Jdbc.use_scrollable_resultset ">false</prop>

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

Find a good article, my previous problems can be found here. In fact, the key to these problems is that hibernate4 and hibernate3 the change of Session management.

Spring also makes a corresponding change ....

Fatal error 1:java.lang.noclassdeffounderror:org/hibernate/cache/cacheprovider

Cause: Spring's sessionfactory and TransactionManager are different from supporting 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 removes hibernatedaosupport, including data access, which does not require hibernatetemplate, which means that the DAO needs to be rewritten, Use Hibernate's session and query interface directly.

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

Error 3:nested exception is Org.hibernate.HibernateException:No Session found for current thread

Cause: It is found that some beans are unable to get the current session and need to elevate the transaction of some previous methods 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 is not_support-->

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

<!--The 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

Cause: Because of the opensessioninview filter problem, 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 the HIBERNATE4 has been fully able to implement the transaction, and Spring3.1 in the Hibernatedao,hibernatetemplete and so on conflict, so Spring3.1 has not provided hibernatedaosupport, Hibernatetemplete, can only use hibernate in the original way with the session:

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

Note that you must also match the parent class Baseserviceimpl when configuring the transaction, or there will be an error:no Session foundfor the currentthread, previously unwanted

The background implementation of sessionfactory.getcurrentsession () is pluggable. As a result, a new extension interface (Org.hibernate.context.spi.CurrentSessionContext) is introduced and

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

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   uses  hibernate4, and when you do not use Opensessioninview mode, you have the following problems when using Getcurrentsession ():  When there is a method list  propagation behavior is supports, it is thrown when the list method is called in another Method GetPage () (No transaction) org.hibernate.hibernateexception: no  session found for current thread  exception.   This is because getcurrentsession () does not automatically create one without a session, and does not know if this is a Spring3.1 implementation bug.   Therefore the best solution is to use the propagation behavior of the required .

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.