Hibernate映射解决问题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Hibernate映射解决问题

做Hibernate映射已经做了五天了,期间遇到了不少错误,有的时候错误很细小,很难发现.现在就来总结一下,常见的错误,有的也是在网上搜了看到的!

第一种问题:Could not execute JDBC batch update

网上说有两种可能:

1.因为Hibernate Tools(或者Eclipse本身的Database Explorer)生成*.hbn.xml工具中包含有catalog="***"(*表示数据库名称)这样的属性,将该属性删除就可以了

2.估计是你的列名里面有关键字的原因吧,命名列的时候不要单独使用date,ID...这种关键字

但是,我觉得他写得不完全啦!我遇到的就不是两种原因,而是List映射时,对应表的索引项没有设成主键,所以就出错了.虽然找了蛮长时间的.同样,

它也会出现Duplicate entry '1' for key 1的错误啦!

第二种问题(归纳下):

1.Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

如果出现这行错误说明你的XML配置文件有不规范的字符,检查下。

2.net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml

如果出现这行错误说明你的hibernate的XML配置文件有错

3.net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found

如果出现这行错误说明hibernate的XML配置文件没有找到,你应该把XML文件放在与你的类文件同个目录下,一般跟同名的java持久化类放在一起,也就是跟Hello_Bean.class类文件一起。4.net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean

如果出现这行错误说明你的xml文件里设置的字段名name的值与Hello_Bean.Java类里的getXXX 或setXXX方法不一致。

5.net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver:没有找到数据库驱动程序

6.The database return no natively generate indentity value。主键没有添加增量

7.net.sf.hibernate.PropertyValueException:not-null property references a null or transient value:com.pack.Rordercontent.

异常产生原因:Rordercontent对象的非空属性Rordertable引用了一个临时对象。

8.net.sf.hibernate.TransientobjectException:objiect references an unsaved transient instance –save the transient instance before flushing: com.pack.Rordertable

持久化对象的某个属性引用了一个临时对象Rordertable

.sf.hibernate.MappingException

当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误。

当出现net.sf.hibernate.MappingException: Resource: …not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。

10. net.sf.hibernate.PropertyNotFoundException

当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for

property name in class …时,

原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。

11. org.hibernate.id.IdentifierGenerationException

当出现org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():异常时,

一般是因为<id>元素配置不正确,<id>元素缺少其子元素<generator></generator>的配置引起。

解决方案:<id>元素映射了相应数据库表的主键字段,对其子元素<generator class="">,其中class的取值可以为increment、identity、sequence、hilo、native……等,

更多的可参考hibernate参考文档,一般取其值为native 。具体可参考2.2.2.1小节。

12. a different object with the same identifier value was already associated with the session

当出现a different object with the same identifier value was already associated with the session时,一般是因为在hibernate中同一个session里面有了两个相同标识但是是不同实体。

有如下几种解决方案:

(1)使用session.clean(),如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。

(2)使用session.refresh(object),当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,

如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。(3)session.merge(object),Hibernate里面自带的方法,推荐使用。

13. SQL Grammer Exception,Could not execute JDBC batch update

当出现SQL Grammer Exception,Could not execute JDBC batch update异常时,一般是由如下问题引起:

(1)SQL语句中存在语法错误或是传入的数据有误;

(2)数据库的配置不合法,或者说是配置有误。较容易出现的有数据表的映射文件(,hbm.xml 文件)配置有误;Hibernate.cfg.xml文件配置有误;

(3)当前的数据库用户权限不足,不能操作数据库。以是以Oracle 数据库为例,

这种情况下在错误提示中会显示java.sql.BatchUpdateException: orA-01031: insufficient privileges 这样的信息。

14.net.sf.hibernate.HibernateException:identifier of an instance of my chapter.pack6.Rordertable altered from 1 to 100:

企图修改处于持久化状态的对象的OID。修改了处于持久化对象的OID在Session清理缓存时就会抛出此异常(对象处于持久化状态时。不允许程序随意修改它的OID。

注意:无论java对象处于临时状态、持久化状态还是游离状态,应用程序都不应该修改它的OID。

相关文档
最新文档