您的位置:首页 > 其它

论两个关于*.hbm.xml配置异常的解决(转)

2010-04-27 10:37 295 查看
论两个关于*.hbm.xml配置异常的解决(转)

最近在做一个小的web程序时,遇到了一些小问题,在经过小弟的一番努力后都一一化解了,欣喜之余就想把这些解决心得择取一二与大家共享。这些问题都比较初级,高手就不必看了,以免浪费时间,呵呵。

异常1:*.hbm.xml 配置文件中<id>元素配置异常

抛出异常描述:

org.springframework.orm.hibernate3.HibernateSystemException: ids for this cla

ss must be manually assigned before calling save(): com.ebupt.ncsp.assist.ser

vice.department.domain.Department; nested exception is org.hibernate.id.Ident

ifierGenerationException: ids for this class must be manually assigned before

calling save(): com.ebupt.ncsp.assist.service.department.domain.Department

org.hibernate.id.IdentifierGenerationException: ids for this class must be ma

nually assigned before calling save(): com.ebupt.ncsp.assist.service.departme

nt.domain.Department

出错或异常原因:

<id>元素配置不正确,<id>元素缺少其子元素<generator></generator>的配置。

解决方法:

<id>元素映射了相应数据库表的主键字段,对其子元素<generator class=””>,其中class的取值可以为increment、identity、sequence、hilo、native……等,更多的可参考hibernate 参考文档,一般取其值为native 。

在配置* .hbm.xml文件时我没有使用自动生成工具,直接手动地配置,这也是我一时粗心,加之对hibernate参考不是很熟,只是依葫芦画瓢,建议大家(新手)多看看参考文档,是很有帮助的。

异常2:*.hbm.xml 配置文件中<id>元素配置异常

抛出异常描述:

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问题就解决了。

其实上述的两个异常在使用自动生成配置文件的工具来映射时是不会遇到的,我在这里举出这两个例子希望对大家能够有所帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐