我对 Session.load上的JavaDocs感到有点困惑:

Return the persistent instance of the given entity class with the given identifier,assuming
that the instance exists. This method might return a proxied instance that is initialized on-demand,
when a non-identifier method is accessed.

You should not use this method to determine if an instance exists (use get() instead). Use this
only to retrieve an instance that you assume exists,where non-existence would be an actual error.

我明白我应该使用get,但我不明白的是当使用非标识符方法时,根据需要初始化它的含义是什么意思.

对我来说,如果我有一个类并使用load(MyClass.class,NonExistingID),然后在返回的实例上打印getId()的输出,它似乎每次都会自动生成一个带有NonExistingID的新实例.为什么是这样?

我只是想了解,getId()是一种非识别方法吗?

解决方法

“非标识符方法”表示返回对象之外的标识符(如主键id中)的方法. load为您提供代理服务器,代理服务器只有在您询问数据库以外的其他内容时才会查询数据库.所以getId是一个标识符方法,Hibernate不会在数据库中查询它的值(它没有,因为你把它传递给了load方法调用).

在the hibernate forums找到此代码段:

An important scenario under which you need to contrast the load and
get methods of the Hibernate Session has to do with what happens when
you provide a primary key that doesn’t actually exist in the database.
Well,with the get method,you are simply returned a null object,
which is no big deal.

With the load method,there’s also no initial problem when you provide
an invalid primary key to the method. From what you can tell,
Hibernate appears to hand you back a valid,non-null instance of the
class in which you are interested. However,the problems start when
you actually try to access a property of that instance – that’s where
you run into trouble.

Remember how I said the load method doesn’t hit the database until a
property of the bean is requested? Well,if you’ve provided a primary
key that doesn’t exist in your database to the load method,when it
does go to the database for the first time,it won’t be able to find
the non-existent,associated record,and your code will cough up big
time. In fact,looking up a field based upon a non-existent primary
key with the Hibernate Session’s load method triggers the following
error:

org.hibernate.ObjectNotFoundException: No row with the given
identifier exists: [User#123]

所以听起来你已经使用load来获取一个不存在的对象的代理,但是由于你没有在其上调用任何’nonidentifier方法’,你没有强制代理命中数据库并且没有出现错误.

java – Hibernate的load()方法对不存在的ID做了什么?的更多相关文章

  1. Hibernate 主清单文件配制的详细介绍

    这篇文章主要介绍了Hibernate 主清单文件配制的详细介绍的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

  2. Hibernate 查询方式总结

    get() and load() HQL 等查询方式

  3. jsp hibernate的分页代码第1/3页

    在查询分页代码中使用Hibernate的一大好处是,既兼顾了查询分页的性能,同时又保证了代码在不同的数据库之间的可移植性。

  4. JSP 开发之hibernate配置二级缓存的方法

    这篇文章主要介绍了JSP 开发之hibernate配置二级缓存的方法的相关资料,这里提供两种配置二级缓存的方法,需要的朋友可以参考下

  5. jsp hibernate 数据保存操作的原理

    当执行到session.save()方法时,Hibernate并不会马上生成insert SQL语句来进行数据的保存,而是当稍后清理session的缓存时才有可能执行insert SQL语句,那么session.save()方法到底会执行哪些步骤呢?

  6. JSP开发之hibernate之单向多对一关联的实例

    这篇文章主要介绍了JSP开发之hibernate之单向多对一关联的实例的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下

  7. 详解hibernate自动创建表的配置

    这篇文章主要介绍了详解hibernate自动创建表的配置的相关资料,需要的朋友可以参考下

  8. Spring Boot深入学习数据访问之Spring Data JPA与Hibernate的应用

    Spring Data JPA是Spring Data的子项目,在使用Spring Data JPA之前,先了解一下Hibernate,因为Spring Data JPA是由Hibernate默认实现的

  9. Hibernate实体对象继承的三种方法

    这篇文章主要介绍了Hibernate实体对象继承的方法的相关资料,需要的朋友可以参考下

  10. Hibernate识别数据库特有字段实例详解

    这篇文章主要介绍了Hibernate识别数据库特有字段实例详解的相关资料,需要的朋友可以参考下

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部