Hibernate 中 load() 与 get() 的优势是什么? [英] What's the advantage of load() vs get() in Hibernate?
本文介绍了Hibernate 中 load() 与 get() 的优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我 load()
与 get()
在 Hibernate 中的优势是什么?
解决方案
load() vs get() 的优势是什么) 在休眠中?
加载() | get() |
---|---|
只有在确定对象存在时才使用 load() 方法. | 如果您不确定该对象是否存在,请使用 get() 方法之一. |
load() 如果在数据库中找不到唯一 id,方法将抛出异常. | get() 如果在数据库中找不到唯一 id,方法将返回 null. |
load() 默认只返回一个代理,在第一次调用代理之前不会命中数据库. | get() 将立即命中数据库. |
例如:
如果我们调用 session.load(Student.class,new Integer(107));
hibernate 会在内存中创建一个 id 为 107 的伪 Student 对象 [row],但 Student 类的剩余属性甚至不会被初始化.
Can anyone tell me what's the advantage of load()
vs get()
in Hibernate?
解决方案
Whats the advantage of load() vs get() in Hibernate?
load() | get() |
---|---|
Only use load() method if you are sure that the object exists. |
If you are not sure that the object exist, then use one of get() methods. |
load() method will throw an exception if the unique id is not found in the database. |
get() method will return null if the unique id is not found in the database. |
load() just returns a proxy by default and database won't be hit until the proxy is first invoked. |
get() will hit the database immediately. |
Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting a database.
For Example:
If we call session.load(Student.class,new Integer(107));
hibernate will create one fake Student object [row] in the memory with id 107, but remaining properties of Student class will not even be initialized.
这篇关于Hibernate 中 load() 与 get() 的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文