Hibernate映射导致的几个异常

 

Hibernate映射导致的几个异常

异常1not-null property references a null or transient value
解决方法:将一对多关系中的方,not-null设置为false

异常2org.hibernate.TransientObjectException: object references an unsaved transient instance
解决方法:cascade="save-update,persist"

异常3org.hibernate.QueryException: could not resolve property
解决方法:"from Category category where category.userID = :userID"修改为"from Category category where userID = :userID"或者"from Category category where category.user.id = :userID"

异常4could not initialize proxy - the owning Session was closed
解决方法:设置lazyfalse

 

 

 

1. org.hibernate.MappingException

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

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

-------------------------------------------------------------------------------------------

2. org.hibernate.PropertyNotFoundException

   当出现org.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …时,原因一般是因为XML   映射文件中的属性与对应的Java类中的属性的gettersetter方法不一致。

-------------------------------------------------------------------------------------------

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的取值可以为incrementidentity     sequencehilonative……等,更多的可参考hibernate参考文档,一般取其值为native

-------------------------------------------------------------------------------------------

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.clear(),如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two              representations of same collection"异常。

 2)先session.clear(),再session.delete(Object),最后session.refresh(object)。当object不是数据库中已有数据的对象的时候,不         能使用session.refresh(object)因为该方法是从hibernatesession中去重新取object,如果session中没有这个对象,则会报错所以        当你使用saveOrUpdate(object)之前还需要判断一下。

 3session.merge(object)Hibernate里面自带的方法,推荐使用。

-------------------------------------------------------------------------------------------

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

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

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

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

   3 当前的数据库用户权限不足,不能操作数据库。以是以Oracle 数据库为例,这种情况下在错误提示中会显示        java.sql.BatchUpdateException: ORA-01031: insufficient privileges这样的信息。

-------------------------------------------------------------------------------------------

6.com/opensymphony/oscache/base/NeedsRefreshException

 没有在工程中加入oscache-2.1.jaroscache-2.3.2.jar

 

posted on 2008-11-17 21:03  cyhjason 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jason-one/archive/2008/11/17/1335388.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值