java - 在Hibernate中,除了如何处理坏ID之外,session.get()和session.load()之间还有什么区别吗?

标签 java hibernate session

我正在开发的应用程序使用 session.load() 通过标识符执行所有查询。考虑到当找不到标识符并引发异常时会造成困惑,我正在考虑将其交换到 session.get()。但在我这样做之前,它是系统的一个基本部分,我想确保这两种方法之间绝对没有其他区别。除了处理无效 ID 之外,您是否还能想到为什么原始开发人员会选择 load() 而不是 get()?

编辑:如上所述,我完全知道 get 返回 false 并且 load 抛出异常。我问的是它们是否还有其他不同之处。

最佳答案

难道 Get 永远不会返回代理,而 Load 会返回代理吗?

http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx

我认为这很重要:

Why is this useful? Well, if you know that the value exist in the database, and you don’t want to pay the extra select to have that, but you want to get that value so we can add that reference to an object, you can use Load to do so: The code above will not result in a select to the database, but when we commit the transaction, we will set the CustomerID column to 1. This is how NHibernate maintain the OO facade when giving you the same optimization benefits of working directly with the low level API.

来自 NH 2.0 引用文档:

Note that Load() will throw an unrecoverable exception if there is no matching database row. If the class is mapped with a proxy, Load() returns an object that is an uninitialized proxy and does not actually hit the database until you invoke a method of the object. This behaviour is very useful if you wish to create an association to an object without actually loading it from the database.

If you are not certain that a matching row exists, you should use the Get() method, which hits the database immediately and returns null if there is no matching row.

关于java - 在Hibernate中,除了如何处理坏ID之外,session.get()和session.load()之间还有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1953462/

相关文章:

java - Java 8 中的偏函数应用

java - 调整多线程的公平性

java - hibernate/GORM : collection was not processed by flush()

PHP Sessions 过期时间 - 在特定的分钟/小时/天数内保持 session 有效

java - 为什么 Apache CloseableHttpResponse 在关闭时不使用实体?

java - 如何在我的 Android 设备上运行我的 Android 应用程序?

java - Hibernate 添加到 List 时重新创建连接表

hibernate - 在 J2SE 中使用 JPA 2.0 @Cacheable 与 Spring、EHCache 和 Hibernate,无需 Hibernate 特定注释

PHP session (用户/管理员)用户级别

PHP 重定向回上一页