Pregunta

Tengo una lista de categoría. Necesito una lista de categorías excluyendo la fila 2,3. ¿Podemos lograr mediante la hibernación mediante el uso de criterios y restricciones?

¿Fue útil?

Solución

Su pregunta no está clara. Suponiendo " Categoría " es una entidad raíz y '' 2,3 '' son identificadores (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 .

Otros consejos

 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();

Para los nuevos Criterios desde la versión Hibernate 5.2:

CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);

List<Long> ids = new ArrayList<>();
ids.add(2L);
ids.add(3L);

Root<Comment> root = getRoot(criteriaQuery);
Path<Object> fieldId = root.get("id");
Predicate in = fieldId.in(ids);
Predicate predicate = criteriaBuilder.not(in);

criteriaQuery
        .select(root)
        .where(predicate);

List<Comment> list = getSession()
        .createQuery(criteriaQuery)
        .getResultList();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top