Hibernate中常见的异常
来源:互联网 发布:mac 怎么删除软件 编辑:程序博客网 时间:2024/05/21 18:40
本文总结Hibernate中常见的异常。
1. net.sf.hibernate.MappingException
当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误。
当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。
2. net.sf.hibernate.PropertyNotFoundException
当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。
3. 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小节。
4. 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"异常。
(3)session.merge(object),Hibernate里面自带的方法,推荐使用。
5. SQL Grammer Exception,Could not execute JDBC batch update
当出现SQL Grammer Exception,Could not execute JDBC batch update异常时,一般是由如下问题引起:
(1)SQL语句中存在语法错误或是传入的数据有误;
1. net.sf.hibernate.MappingException
当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误。
当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。
2. net.sf.hibernate.PropertyNotFoundException
当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。
3. 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小节。
4. 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里面自带的方法,推荐使用。
5. 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这样的信息。
针对上面的各种原因,开发人员可以找出对应的解决方案
文章来源:http://bbs.ysaas.com/thread-1156-1-1.html
- Hibernate中常见的异常
- Hibernate中常见的异常总结
- hibernate常见的错误异常
- hibernate常见的异常解决
- Hibernate遇到的几个常见映射异常
- Hibernate的几个常见异常及解决方法
- Hibernate的几个常见异常及解决方案
- hibernate常见异常
- Hibernate常见异常总结
- hibernate常见异常!
- hibernate常见包异常
- hibernate常见异常
- Hibernate 常见异常
- hibernate常见异常
- Hibernate常见异常总结
- hibernate常见异常总结
- Hibernate 常见异常处理
- hibernate常见异常
- loadexe (delphi)
- 一个向别的进程插入dll的代码
- PHP初学者头疼问题总结
- 迷茫中,寻找方向
- P2P简单技术原理
- Hibernate中常见的异常
- eclipse 插件安装方式 link 方式
- 总帐接口表详细说明
- spring配置文件笔记
- Csdn Blog 修复了编辑器不是别<br/>的问题。
- WebService简单原理(转载)
- Tomcat安装为服务(service)的问题[转].
- Oracle数据库分区表操作方法
- AVIVO HD与PUREVIDEO HD技术对比介绍