hibernate配置文件异常的排除

最近在做一个小的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问题就解决了。

其实上述的两个异常在使用自动生成配置文件的工具来映射时是不会遇到的,我在这里举出这两个例子希望对大家能够有所帮助。

不知道来年,会不会开出一地的记忆和忧愁。

hibernate配置文件异常的排除

相关文章:

你感兴趣的文章:

标签云: