`
wj98127
  • 浏览: 263802 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate中No row with the given identifier exists

    博客分类:
  • Java
阅读更多
    今天整理权限和菜单关联的时候,报出了No row with the given identifier exists,查了半天中间表,找了一些资料,转过来记载一下

   
作者:blurxx 日期:2008-01-17

产生此问题的原因:
         有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique="true">(特殊的多对一映射,实际就是一对一)来关联table2.当hibernate查找的时候,table2里的数据没有与table1相匹配的,这样就会报No row with the given identifier exists这个错.(一句话,就是数据的问题!)
         假如说,table1里有自身的主键id1,还有table2的主键id2,这两个字段.
         如果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,如果找不到就抛出异常
分享到:
评论
1 楼 QQ371496669 2012-04-27  
引用
load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常

有一种简单的方法可以使它不报异常,而跟get方式一样返回空值,那就是:
配置文件中many-to-one或one-to-one里设置not-found="ignore"属性
not-found默认为 exception
not-found意思是:指定外键引用的数据不存在时如何处理

相关推荐

    no resource

    fe ge 2007 programe

    Hibernate+中文文档

    3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. ...

    apktool_AndResGuard:修复apktool打包出现No resource found...

    No resource found that matches the given name..... ....... 使用apktool-2.0.3也可以回编译,但是比如微信回编译后会增加到110M左右,很不友好。 而使用apktool-2.3.2和原包大小一样,65M左右。 基于apktool

    Hibernate中文详细学习文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    hibernate3.2中文文档(chm格式)

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    Hibernate中文API

    1.2. 第一部分 - 第一个Hibernate应用程序 首先我们将创建一个简单的基于控制台的(console-based)Hibernate应用程序。由于我们使用Java数据库(HSQL DB),所以不必安装任何数据库服务器。 假设我们希望有一个小...

    Hibernate 中文 html 帮助文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    The Digital Object Identifier initiative: metadata implications

    The Digital Object Identifier initiative: metadata implications Context/status information The purpose of this document is to generate a DOI Foundation consensus paper on metadata, as a companion ...

    不常用的TParser类解析(文本文件中相关字符串)

    Raises an EParserError exception with the given message procedure ErrorFmt(); Raises an EParserError exception and formats the message. procedure ErrorStr(); Raises an EParserError exception with ...

    hibernate.properties

    ## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager #hibernate.transaction.manager_lookup_class org.hibernate.transaction....

    HibernateAPI中文版.chm

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    Hibernate Tutorial 03 (Object Identifier).pdf

    上web tools课时老师发的关于hibernate的教程之一。大概内容看标题就知道了。需要注意的是文档全部是英文的。英文不好的童鞋就不要下了。。。

    微软内部资料-SQL性能优化5

    If there is no clustered index, there is a sysindexes row for the table with an indid value of 0, and that row will keep track of the address of the first IAM for the table. The IAM is a giant bitmap...

    Hibernate Reference Documentation3.1

    1.2. Part 1 - The first Hibernate Application 1.2.1. The first class 1.2.2. The mapping file 1.2.3. Hibernate configuration 1.2.4. Building with Ant 1.2.5. Startup and helpers 1.2.6. Loading and ...

    windows程序设计

    created with the default settings in Microsoft Visual C++ 6 with one major exception: aside from other compiler and linker flags, the executable in the DEBUG directory has been compiled with the ...

    servlet2.4doc

    Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. getAttribute(String) - Method in interface javax.servlet.http.HttpSession Returns the object ...

    最全Hibernate 参考文档

    1. 在Tomcat中快速上手 1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. 架构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 3. 配置 ...

    Hibernate注释大全收藏

    • Single Table per Class Hierarchy Strategy: the &lt;subclass&gt; element in Hibernate 每个类层次结构一张表 • Joined Subclass Strategy: the &lt;joined-subclass&gt; element in Hibernate 连接的子类策略 @...

    hibernate 教程

    提供一个标识属性(identifier property)(可选) 4.1.4. 建议使用不是final的类 (可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode() 4.4. 持久化生命周期(Lifecycle)中的回调...

    Hibernate_二级缓存总结

    HIBERNATE 中的 CACHE 有两级 . 一级是在 Session 范围内的 CACHE . 即每个 Session 有自己的一个 CACHE, 当前操作的对象都会被保留在 CACHE 中 . 但是 Session 关闭后这个 CACHE 也就没有 . 可见这级 CACHE 的生命...

Global site tag (gtag.js) - Google Analytics