hibernate2 升級為hibernate3的需要注意的事項貼)

來源:互聯網
上載者:User

前言:一篇不錯的文章,同樣適用於用middlegen產生的hbm.xml檔案,尤其最後一條


目的,主要是那當前的項目練手,熟悉一下hibernate2和hibernate3的差別,給當前項目一點擴充的空間. 

1.首先將hibernate2.jar替換為hibernate3.jar(hibernate-3.0.5)

hibernate-tools.jar也替換成新的(從hibernate-tools-3.0.0.alpha4a找出來的)

2.將所有程式中的net.sf.hibernate替換為org.hibernate.

 

3.但是有例外

net.sf.hibernate.expression.Expression換為org.hibernate.criterion.Expression

如果用eclipse,用ctrl+shift+o快速鍵可以加快速度

 

4.在使用hql查詢時將

createSQLQuery(hql,"c",EZCampaignDTO.class);改為createSQLQuery(hql).addEntity("c",EZCampaignDTO.class);

 

5.在批量插入時

將原來的int size = ((SessionFactoryImpl)(session.getSessionFactory())).getJdbcBatchSize()

改為int size = ((SessionFactoryImpl)(session.getSessionFactory())).getSettings().getJdbcBatchSize();

 

6.在計算count時

將原來的int size = ((Integer) session.iterate(hql).next()).intValue();

改為int size = ((Integer) session.createQuery(hql).iterate().next()).intValue();

其中hql="select count(*) from " + DAOVar.contactClass;

 

7.還有就是把.hbm中的hibernate-mapping-2.0.dtd替換為hibernate-mapping-3.0.dtd

Hibernate Mapping DTD 2.0替換為Hibernate Mapping DTD 3.0

 

8.hibernate.cfg.xml中

Hibernate Mapping DTD 2.0替換為Hibernate Mapping DTD 3.0

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

 

9.hibernate.properties中類似

 

10.cache-config.xml中

<provider className="net.sf.hibernate.cache.OSCacheProvider"/>替換為

<provider className="org.hibernate.cache.OSCacheProvider"/>

 

11.classeshibernate.properties中

hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

hibernate.dialect=org.hibernate.dialect.SQLServerDialect

暈s了,怎麼這裡還有

還是用編輯器暴力替換一下吧乾脆

 

然後部署,整合測試,希望一切ok

結果咣鐺,還是報錯

 

12.在自動外部模組部分有一個功能是根據模版自動產生.hbm檔案在load,結果出來的.hbm中有問題:

產生的 <composite-id unsaved-value="any" mapped="false">其中mapped="false" 出錯.

找了半天才發現在網上的hibernate-mapping-3.0.dtd檔案有支援mapped="false"這個屬性.而本地的hebernate3.0.5中的

hibernate-mapping-3.0.dtd檔案沒有這個屬性.暈,hibernate也太不負責了吧. 解決辦法把hibernate-mapping-3.0.dtd

 copy到jboss/bin目錄下然後,在template檔案中

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "hibernate-mapping-3.0.dtd">

然後他會在jboss/bin目錄下讀取該檔案

 

13.重新測試,還是咣鐺

發現子類讀父類資料時拋出異常

"org.hibernate.LazyInitializationException: could not initialize proxy"

延遲抓取出的錯,hb3對many-to-one的預設處理是lazy = "proxy"

沒有搞懂到底怎麼回事,把所有many-to-one,one-to-one都加上lazy="false"

 

再測試終於大功告成

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.