讓hibernate4與3在配置文件中的區別有幾點:
1、獲取sessionfactory的方式:
在hibernate3中獲取sessionfactory的方式:
<bean id="sessionFactory"class="org.sPRingframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
在hibernate4中則是使用這種方式:
<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
2、事物的使用:
hibernate3:
<bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
hibernate4:
<bean id="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
3、使用緩存機制:
hibernate3:
<prop key="hibernate.current_session_context_class">thread</prop>
hibernate4:
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
4、
在hibernate3中可以使用hibernate自帶的Dao組件,如:HibernateTemplete/HibernateDaoSupport等;
在hibernate4中這些有取消了,必須使用getCurrentSession()來獲取Session;
5
getCurrentSession()事務會自動關閉,所以在有所jsp頁面查詢數據都會關閉session。要想在jsp查詢數據庫需要加入: org.springframework.orm.hibernate4.support.OpenSessionInViewFilter過濾器。
6、
Hibernate分頁出現 ResultSet may only be accessed in a forward direction 需要設置hibernate結果集滾動
<prop key="jdbc.use_scrollable_resultset">false</prop>
7、在單獨使用hibernate4的使用:獲取工廠的方式變為:
ServiceRegistry resgistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(resgistry);
Session session = factory.openSession();
當然你也可以通過設置單例的工具類來簡便過去方式,具體工具類你可以查閱hibernate4的參考文檔;
新聞熱點
疑難解答