简体   繁体   中英

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

UPDATE

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() . This will give you a number of rows, but it will be an Object . You can cast it to (Number) and use intValue() method to get an int . At the end compare it to 1 and you're done! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM