[英]What's the difference between query.uniqueResult() vs session.load() in Hibernate?
Can anyone tell me what's the difference between this code: 谁能告诉我这段代码有什么区别:
// This following method checks if there is an open session
// and if yes - returns it, if not - opens a new session.
Session session = getSession();
Query query = session.createQuery("from Entity e where e.id = 1");
Entity object = (Entity)query.uniqueResult();
and this: 和这个:
Session session = getSession();
Entity object = (Entity)session.load(Entity.class, new Integer(1));
Does the first method return a proxy object? 第一个方法是否返回代理对象? And if I call it again does it hit the database? 如果我再次调用它,它会打到数据库吗?
There are some differences (as of Hibernate 5.2.6). 存在一些差异(从Hibernate 5.2.6开始)。
session.load()
query.uniqueResult()
null
if the query returns no results. 返回与查询匹配的单个实例的便捷方法,如果查询未返回结果,则返回null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.