[英]Hibernate query Count logic
public boolean minimoUmSuperUsuario(Usuario usuario, Session sessionExterna) throws HibernateException {
Criteria crit = sessionExterna.createCriteria(Usuario.class);
return (Long) crit.uniqueResult() > 0;
}
Heres what I need to do: 这是我需要做的:
select * from usuario where superusuario and usuarioativo = true
if superUsuario and usuarioAtivo == 1 return true
else false.
I dont know how to put this in the method above 我不知道如何将其放在上面的方法中
This is what i have done 这就是我所做的
public boolean superUsuarioUm(Usuario usuario, Session sessionExterna) throws HibernateException {
Criteria crit = sessionExterna.createCriteria(Usuario.class);
crit.add(Restrictions.eq("superUsuario", usuario.isSuperUsuario()));
crit.add(Restrictions.eq("usuarioAtivo", usuario.isUsuarioAtivo()));
final Number value = (Number) crit.uniqueResult();
if (value.intValue() <= 1) {
return true;
} else {
return false;
}
}
Does this seem correct? 这看起来正确吗?
You simply need to execute the query and get the result. 您只需要执行查询并获得结果。
Criteria crit = // your criteria from your post
final Number value = (Number) crit.uniqueResult();
return value.intValue() <= 1;
All you need to do to get the result from database is use crit.uniqueResult()
. 从数据库获取结果所需要做的就是使用
crit.uniqueResult()
。 This will give you a number of rows, but it will be an Object
. 这将为您提供许多行,但它将是一个
Object
。 You can cast it to (Number)
and use intValue()
method to get an int
. 您可以将其
intValue()
为(Number)
并使用intValue()
方法获取int
。 At the end compare it to 1
and you're done! 最后,将它与
1
进行比较就可以完成! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.