程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate配置文件異常的排除

hibernate配置文件異常的排除

編輯:關於JAVA

最近在做一個小的web程序時,遇到了一些小問題,在經過小弟的一番努力後都一一化解了,欣喜之余就想把這些解決心得擇取一二與大家共享。這些問題都比較初級,高手就不必看了,以免浪費時間,呵呵。

異常1:*.hbm.xml 配置文件中元素配置異常

拋出異常描述:

org.springframework.orm.hibernate3.HibernateSystemException: ids for this class must be manually assigned before calling save(): com.ebupt.ncsp.assist.service.department.domain.Department; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned beforecalling save(): com.ebupt.ncsp.assist.service.department.domain.Departmentorg.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.ebupt.ncsp.assist.service.department.domain.Department

出錯或異常原因:

元素配置不正確,元素缺少其子元素的配置。

解決方法:

元素映射了相應數據庫表的主鍵字段,對其子元素,其中class的取值可以為increment、identity、sequence、hilo、native……等,更多的可參考hibernate 參考文檔,一般取其值為native 。

在配置* .hbm.xml文件時我沒有使用自動生成工具,直接手動地配置,這也是我一時粗心,加之對hibernate參考不是很熟,只是依葫蘆畫瓢,建議大家(新手)多看看參考文檔,是很有幫助的。

異常2:*.hbm.xml 配置文件中元素配置異常

拋出異常描述:

org.springframework.orm.hibernate3.HibernateSystemException:exception setting property value with CGLIB(set hibernate.cglib.use-reflection-optimizer=false for more info) setter of com.info.beran.com.SetComCode nested exception is org.hibernate.PropertyAccessException: setting property value with CGLIB(set hibernate.cglib.use-reflection-optimizer=false for more info)

出錯或異常原因:

1. 數據庫表中的字段(一般都是數值型的字段)的類型與配置文件中所配置的類型不一致。

2. 在數據庫中,數值型字段的值為空null。

相應的解決:

1. 將該字段在配置文件中的類型修改成一致的類型都為Integer。

2. 因為null不能賦值給原始類型,所以在配置數據庫中數值型的字段類型(long、int、tinyint)都映射成對象類型Integer問題就解決了。

其實上述的兩個異常在使用自動生成配置文件的工具來映射時是不會遇到的,我在這裡舉出這兩個例子希望對大家能夠有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved