【问题标题】:Is Hibernate query.uniqueResult() hit data baseHibernate query.uniqueResult() 是否命中数据库
【发布时间】:2017-04-01 00:40:03
【问题描述】:

我使用下面的代码来获取整数值。

Query query = currentSession().getNamedQuery("getUser");
query.setString("password", password);
query.setString("userId", userId);

if (query.uniqueResult() != null && (Integer) query.uniqueResult() = 1 ) {
 ...
}

请解释一下

query.uniqueResult() 

使用缓存和不使用缓存

这是两次命中数据库吗?

如果我们启用缓存会发生什么?甚至它的命中两次数据库?

【问题讨论】:

  • getUser 是获取整数值的奇怪名称。

标签: java spring hibernate hql


【解决方案1】:

是的。您应该将值存储在变量中并检查变量是否为 null/1

【讨论】:

  • 很遗憾没有。我通过使用它并查看我的查询何时以及多久访问数据库来学习它
【解决方案2】:

是的,它会起作用。 是的,它会击中 de DB 两次。

您可能会遵循 XtremeBaumer 的建议,并像这样进行测试:

Integer test = query.uniqueResult();     
if ( test != null && test == 1 ) {
 ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2011-02-02
    • 2014-11-12
    相关资源
    最近更新 更多