Example usage for org.hibernate.criterion Restrictions idEq

List of usage examples for org.hibernate.criterion Restrictions idEq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions idEq.

Prototype

public static Criterion idEq(Object value) 

Source Link

Document

Apply an "equal" constraint to the identifier property

Usage

From source file:com.vg.nplusone.App.java

/**
 * try to use FetchMode = JOIN with criteria
 * it doesn't work as expected/* w w  w  .  ja  v  a2s.c o m*/
 */
private static void readWithJoinCriteria() {
    Session session = NPlusOneHibernateUtil.getSessionFactory().openSession();

    session.beginTransaction();
    User u = (User) session.createCriteria(User.class).setFetchMode("permissions", FetchMode.JOIN)
            .add(Restrictions.idEq(1)).uniqueResult();
    System.out.println(u.getName());
    for (Permision p : u.getPermisions()) {
        System.out.println(p.getRole());
    }

    session.getTransaction().commit();
}

From source file:com.wooki.domain.dao.GenericDAOImpl.java

License:Apache License

@SuppressWarnings("unchecked")
public T findById(PK id) {
    assert id != null;

    Criteria crit = session.createCriteria(getEntityType());
    crit.add(Restrictions.idEq(id));

    return (T) crit.uniqueResult();
}

From source file:com.yahoo.elide.datastores.hibernate3.HibernateTransaction.java

License:Apache License

/**
 * load a single record with id and filter.
 *
 * @param entityClass class of query object
 * @param id id of the query object//from  w  ww  . j a  v  a  2s.  co m
 * @param filterExpression FilterExpression contains the predicates
 * @param scope Request scope associated with specific request
 */
@Override
public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression,
        RequestScope scope) {

    try {
        Criteria criteria = session.createCriteria(entityClass).add(Restrictions.idEq(id));
        if (scope != null && isJoinQuery()) {
            joinCriteria(criteria, entityClass, scope);
        }
        if (filterExpression.isPresent()) {
            CriterionFilterOperation filterOpn = buildCriterionFilterOperation(criteria);
            criteria = filterOpn.apply(filterExpression.get());
        }
        return criteria.uniqueResult();
    } catch (ObjectNotFoundException e) {
        return null;
    }
}

From source file:com.yahoo.elide.datastores.hibernate5.HibernateTransaction.java

License:Apache License

/**
 * load a single record with id and filter.
 *
 * @param entityClass class of query object
 * @param id id of the query object/*  w  ww .ja  va  2s.  c  om*/
 * @param filterExpression FilterExpression contains the predicates
 * @param scope Request scope associated with specific request
 */
@Override
public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression,
        RequestScope scope) {

    try {
        Criteria criteria = session.createCriteria(entityClass).add(Restrictions.idEq(id));
        if (filterExpression.isPresent()) {
            CriterionFilterOperation filterOpn = buildCriterionFilterOperation(criteria);
            criteria = filterOpn.apply(filterExpression.get());
        }
        return criteria.uniqueResult();
    } catch (ObjectNotFoundException e) {
        return null;
    }
}

From source file:cz.jirutka.commons.hibernate.HibernateGenericDAO.java

License:Open Source License

@Override
public boolean isPersistent(Serializable id, Class<? extends Persistable> clazz) {
    Long result = (Long) session().createCriteria(clazz).add(Restrictions.idEq(id))
            .setProjection(Projections.rowCount()).uniqueResult();
    return (result == 1) ? true : false;
}

From source file:de.congrace.blog4j.dao.CategoryDaoImpl.java

License:Apache License

public int getNextOrderValue(Category parent) {
    Criteria crit = getCurrentSession().createCriteria(Category.class);
    if (parent == null) {
        crit.add(Restrictions.isNull("parentCategory"));
    } else {//from  w  ww  .  ja v a2s . co m
        crit.createCriteria("parentCategory").add(Restrictions.idEq(parent.getId()));
    }
    crit.setProjection(Projections.rowCount());
    return ((Integer) crit.uniqueResult()).intValue();
}

From source file:de.congrace.blog4j.dao.CategoryDaoImpl.java

License:Apache License

public Category getCategoryByOrder(Category parent, int order) {
    Criteria crit = this.getCurrentSession().createCriteria(Category.class)
            .add(Restrictions.eq("order", order));
    if (parent == null) {
        crit.add(Restrictions.isNull("parentCategory"));
    } else {/*from  ww w .  j a  va2  s. c o m*/
        crit.createCriteria("parentCategory").add(Restrictions.idEq(parent.getId()));
    }
    return (Category) crit.uniqueResult();
}

From source file:de.congrace.blog4j.dao.CategoryDaoImpl.java

License:Apache License

public List<Category> getCategoriesByParent(Category parent) {
    return (List<Category>) getCurrentSession().createCriteria(Category.class).createCriteria("parentCategory")
            .add(Restrictions.idEq(parent.getId())).addOrder(Order.asc("order")).list();
}

From source file:de.decidr.model.commands.tenant.GetWorkflowInstancesCommand.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from ww w .j av  a 2s  .c o m
public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException {

    String hql = "select t.id from Tenant t where t.id  = :tenantId";
    Boolean tenantExists = evt.getSession().createQuery(hql).setLong("tenantId", getTenantId())
            .uniqueResult() != null;

    if (!tenantExists) {
        throw new EntityNotFoundException(Tenant.class, getTenantId());
    }

    PaginatingCriteria c = new PaginatingCriteria(WorkflowInstance.class, evt.getSession());

    Criteria c2;

    // create criteria 2nd level and project to name
    c2 = c.createCriteria("deployedWorkflowModel", CriteriaSpecification.INNER_JOIN);

    // create criteria 3rd level and project to name
    c2.createCriteria("tenant", CriteriaSpecification.INNER_JOIN).add(Restrictions.idEq(getTenantId()));

    if (paginator != null) {
        paginator.apply(c);
    }

    c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

    result = c.list();
}

From source file:de.evjnw.jlk.work.impl.AnhangDaoImpl.java

License:Apache License

public Anhang lade(int id) throws DaoException {

    Object o;/*from   w  w w  .  ja  va  2  s  .com*/
    try {
        Session session = factory.getCurrentSession();
        Criteria criteria = session.createCriteria(Anhang.class);
        criteria.add(Restrictions.idEq(id));
        o = criteria.uniqueResult();
    } catch (HibernateException e) {
        throw new DaoException("Fehler beim Laden des Anhang mit id=" + id + " : " + e.getMessage(), e);
    }
    if (o == null) {
        throw new DaoException("Kein Anhang vorhanden mit id=" + id);
    }
    Anhang anhang = (Anhang) o;

    return anhang;
}