No row with the given identifier exists 解决方法(集锦)

来源:互联网 发布:二郎神和孙悟空知乎 编辑:程序博客网 时间:2024/05/27 03:25

No row with the given identifier exists 解决方法

出现异常org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists

 

一.

No row with the given identifier exists 解决方法
有两张表,a和b.产生此问题的原因就是a里做了关联<one- to-one>或者<many-to-one unique="true">(特殊的多对一映射,实际就是一对一)来关联b.当hibernate查找的时候,b里的数据没有与a相匹配的,这样就会报No row with the given identifier exists这个错.(一句话,就是数据的问题!)

      假如说,a里有自身的主键id1,还有b的主键id2,这两个字段.

      如果hibenrate设置的单项关联,即使a中的id2为null值,b中id2中有值,查询都不会出错.但是如果a中的id2字段有值,但是这个值在b中主键值里并没有,就会报上面的错!

      如果hibernate是双向关联,那么a中的id2为null值,但是b中如果有值,就会报这个错.这种情况目前的解决办法就是改成单项关联,或者把不对应的数据改对!


二.
hibernate的一些常见的错误

No row with the given identifier exists

表示你现在查询的对象所关联的对象有问题,一般是因为数据的问题(该对象所关联的对象找不到)

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配置文件有不规范的字符,检查下。

net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
如果出现这行错误说明你的hibernate的XML配置文件有错

net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
如果出现这行错误说明hibernate的XML配置文件没有找到,你应该把XML文件放在与你的类文件同个目录下,本文中是放在hibernateclasseshibernate目录下,也就是跟Hello_Bean.class类文件一起。

net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
如果出现这行错误说明你的xml文件里设置的字段名name的值与Hello_Bean.java类里的getXXX或setXXX方法不一致。

net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
如果出现这行错误说明你的MYSQL驱动没有加进JB库里或者不在CLASSPATH里。

三.
这个异常是在 多对一关系映射时,一方表中对应的数据不存在才抛出的。
原来的配置:
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" >
   <column name="userId" unique="true"/>
</many-to-one>

修改后的:
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" not-found="ignore">
   <column name="userId" unique="true"/>
</many-to-one>

红色是修改的部分。意思是当对应的数据不存在时 忽略掉,用null值填充。该属性默认值:exception 。

四.
今天整理权限和菜单关联的时候,报出了No row with the given identifier exists,查了半天中间表,找了一些资料,转过来记载一下


产生此问题的原因:

         有两张表,table1如用户表user,和table2角色表role.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique="true">(特殊的多对一映射,实际就是一对一)来关联table2.

     当hibernate查找的时候,table2角色表role里的数据没有与table1用户表user相匹配时,这样就会报No row with the given identifier exists这个错.(一句话,就是数据的问题!)

       假如说,table1用户表user里有自身的主键id1,还有table2角色表role的主键id2(其实是table2角色表role的外键),这两个字段.
         如果hibenrate设置的单项关联,即使table1中的id2为null值,table2中id2中有值,查询都不会出错.但是如果table1中的id2字段有值,但是这个值在table2中主键值里并没有,就会报上面的错!

         如果hibernate是双向关联,那么table1中的id2为null值,但是table2中如果有值,就会报这个错.这种情况目前的解决办法就是改成单项关联,或者把不对应的数据改对! (我就是这样改好的)

           这就是报这个错的原因了,知道原因了就相应的改就行了.或许还有些人迷惑hibernate关联都配好了,怎么会出现这样的错?其实这是编程的时候出现的问题,假如说我在添加信息的时候,页面传过来的struts的formbean到dao方法中需要封装成hibernate的po(就是hibenrate的bean),要是一个个po.get(form.set())实在太麻烦了,这样一般都会写个专门的方法来封装,遇到po.get(form.set())这种情况直接把struts的formbean对象传到此方法中封装就行了,假如我有个字段是创建人id,那么这个字段是永远不会改的,我在添加的时候还调用这个方法,这个专门封装的方法是有一些判断的,假如说我判断一下,如果遇到创建人id传过来为空值,我判断如果是空值,我把创建人id设为0,但是用户表中userid是主键从1开始自增的,那么这样数据就对应不上了,一查就会出这个错了.这个错在开发刚开始的时候经常发生,因为每个人的模块都是由相应的人独立开发完成以后再整合在一起的,每个人写单独那一块的时候往往会忽略这些,所以整合的时候这些问题往往就都一下子全冒出来了....整合很辛苦,tnnd!

hibernate的查询的比较
hibernate的查询有很多,Query,find,Criteria,get,load

query使用hsql语句,可以设置参数是常用的一种方式

criteria的方式,尽量避免了写hql语句,看起来更面向对象了。

find方式,这种方式已经被新的hibernate丢弃

get和load方式是根据id取得一个记录
下边详细说一下get和load的不同,因为有些时候为了对比也会把find加进来。

1,从返回结果上对比:
load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常
get方法检索不到的话会返回null

2,从检索执行机制上对比:
get方法和find方法都是直接从数据库中检索
而load方法的执行则比较复杂
1,首先查找session的persistent Context中是否有缓存,如果有则直接返回
2,如果没有则判断是否是lazy,如果不是直接访问数据库检索,查到记录返回,查不到抛出异常
3,如果是lazy则需要建立代理对象,对象的initialized属性为false,target属性为null
4, 在访问获得的代理对象的属性时,检索数据库,如果找到记录则把该记录的对象复制到代理对象的target
上,并将initialized=true,如果找不到就抛出异常

 Hibernate: No row with the given identifier exists
郁闷了好久,终于在goole上找到了错误原因
ObjectNotFoundException: No row with the given identifier exists
Where it can occur:

This might occur if you try to load a non-proxied object with session.load() or you load a proxied object and later access the proxy. It may also occur when loading mapped collections which are not eagerly fetched.
What it means:

This means just what it says - Hibernate expected to have a row with a certain id in the database, and did however not find it.
How it can be caused:
Wrong id for load

If you are using a nonexistant id for a load(), you will get this exception. Note that if you are using proxies, this may occur later on, when the proxy is first "broken".
Broken foreign key relationships

If you map for example a many-to-one relationship, and the table containing the objects on the "many" side contains a foreign key to a non-existant row of the "one" side table, you will get such an exception. This will only happen if the collection is not fetched eagerly.
Reusing a session across multiple transactions

The hibernate session internally memorizes some state. For example it internally stores ids which it assumes are already deleted from the database. So if you make the session believe somehow an object with a certain id has been deleted, and later try to load it again, you will get this exception without Hibernate even trying to access the database. So be careful with that.
How it can be fixed:

    Check the ids you are using for load
    Validate your foreign key relationships - the database should already maintain such integrity constraints.
    Beware of multiple transactions within a single session. Try to stick to "one session, one transaction" rule. Especially do not reuse sessions after an HibernateException.

也就是是说关系数据库一致性遭到了破坏,找到相关表,就可以解决了

原创粉丝点击