本文总结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"异常。
(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://www.blogjava.net/amigoxie/archive/2008/01/07/173342.html
分享到:
相关推荐
hibernate异常文档.xls hibernate异常文档.xls
hibernate 常见异常非常不错的,hibernate 常见异常非常不错的
整理了hibernate常见的异常,有了此文档,开发中遇到的Hibernate异常调试更轻松
在使用HIBERNATE是經常出現的一些錯誤!
hibernate常见异常,hibernatejpa注解使用开发的解决方案
由于Hibernate是基于 JDBC的,所以它的数据库查寻的能力相对于CMP来说也是异常强大的,Hibernate自身也提供了HQL查寻语句。 一个最简单的Hibernate project(不涉及Struts, Tomcat, XDoclet,JBoss等东东)必须的...
Hibernate映射导致的几个常见异常
包含异常现象,说明,处理方法,主要为Java常见的异常,Hibernate常见异常等等
本文档是写的Java相关的异常及其处理办法 包括:Java、Jdbc、Android、JavaScript、Servlet、Jsp、Hibernate、Struts2中一般出现的异常及其解决办法,注明适用于初学者。
Hibernate 删除出现异常的解决方案.doc
一下午才整合成功,书中的bulidSessionFactory()方法在hibernate4.3.5中已经deprecated,这里面有方法实现,用完请给好评。
hibernate4.3.8 自带demo
11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱...
//该方法会延迟加载/获取数据(只有使用该对象中的字段时才会向数据库发出查询语句),该方法会在内部和二级缓存中查找数据,当数据库中没有要查询的数据时报ObjectNotFoundException异常 (7).createQuery(String HQL)...
处理异常 9.8. 生命周期和对象图(Lifecyles and object graphs) 9.9. 拦截器(Interceptors) 9.10. 元数据(Metadata) API 10. 事务和并行(Transactions And Concurrency) 10.1. 配置,会话和工厂...
11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱...
Spring+Hibernate运行时所出现的异常Caused by java.lang.StringIndexOutOfBoundsException String index out解决方法
11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱...
在进行Spring4.1+hibernate4+struts2搭建web环境时,经常出现session数据为空,需要在web.xml添加配置,完成SSH环境搭建,使请求未完全加载之前,一直保持session会话开起。