gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:14:50 26 4
gpt4 key购买 nike

我正在开发的应用程序使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com