¿Cómo lograr "no en" mediante el uso de restricciones y criterios en Hibernar?


Tengo una lista de categorías. Necesito una lista de categoría excluyendo 2,3 fila. ¿Podemos lograr a través de la hibernación mediante el uso de Criterios y Restricciones?

Author: Shashi, 2009-08-03

2 answers

Su pregunta es algo confusa. Suponiendo que "Category" es una entidad raíz y "2,3" son ids (o valores de alguna propiedad de la categoría") puede excluirlos usando lo siguiente:

Criteria criteria = ...; // obtain criteria from somewhere, like session.createCriteria() 
criteria.add(
  Restrictions.not(
     // replace "id" below with property name, depending on what you're filtering against
    Restrictions.in("id", new long[] {2, 3})
  )
);

Lo mismo se puede hacer con DetachedCriteria.

 87
Author: ChssPly76,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-03 17:07:34
 Session session=(Session) getEntityManager().getDelegate();
        Criteria criteria=session.createCriteria(RoomMaster.class);
//restriction used or inner restriction ...
        criteria.add(Restrictions.not(Restrictions.in("roomNumber",new String[] { "GA8", "GA7"})));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        List<RoomMaster> roomMasters=criteria.list();
 1
Author: sourav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-03 12:54:17