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:org.mzd.shap.domain.dao.ProjectDaoSpringHibernate.java

License:Open Source License

public Project findByUserAndId(final User user, final Integer id) {
    return getHibernateTemplate().execute(new HibernateCallback<Project>() {
        public Project doInHibernate(Session session) throws HibernateException, SQLException {
            return (Project) session.createCriteria(getPersistentClass()).addOrder(Order.asc("id"))
                    .add(Restrictions.idEq(id)).createCriteria("users").add(Restrictions.idEq(user.getId()))
                    .uniqueResult();//from   ww  w.j  a v  a  2s  . c  om
        }
    });
}

From source file:org.mzd.shap.domain.dao.SampleDaoSpringHibernate.java

License:Open Source License

public Sample findByProjectAndId(Project project, Integer id) {
    return findUniqueByCriteria(Restrictions.eq("project", project), Restrictions.idEq(id));
}

From source file:org.mzd.shap.domain.dao.SequenceDaoSpringHibernate.java

License:Open Source License

public Sequence findById(final Integer id, boolean minimal) {
    if (!minimal) {
        return getHibernateTemplate().execute(new HibernateCallback<Sequence>() {
            public Sequence doInHibernate(Session session) throws HibernateException, SQLException {
                return (Sequence) session.createCriteria(getPersistentClass()).add(Restrictions.idEq(id))
                        .uniqueResult();
            }// www  .  j  a  va  2s .com
        });
    } else {
        return getHibernateTemplate().execute(new HibernateCallback<Sequence>() {
            public Sequence doInHibernate(Session session) throws HibernateException, SQLException {
                return (Sequence) session
                        .createQuery("SELECT seq.id AS id, seq.version AS version " + "FROM Sequence AS seq "
                                + "WHERE seq.id=:id")
                        .setParameter("id", id).setResultTransformer(Transformers.aliasToBean(Sequence.class))
                        .uniqueResult();
            }
        });
    }
}

From source file:org.mzd.shap.domain.dao.SequenceDaoSpringHibernate.java

License:Open Source License

public Sequence findBySampleAndId(Sample sample, Integer id) {
    return findUniqueByCriteria(Restrictions.eq("sample", sample), Restrictions.idEq(id));
}

From source file:org.mzd.shap.domain.dao.SequenceDaoSpringHibernate.java

License:Open Source License

public Sequence loadWithData(final Integer id) {
    return getHibernateTemplate().execute(new HibernateCallback<Sequence>() {
        public Sequence doInHibernate(Session session) throws HibernateException, SQLException {
            return (Sequence) session.createCriteria(getPersistentClass()).setFetchMode("data", FetchMode.JOIN)
                    .add(Restrictions.idEq(id)).uniqueResult();
        }//from   w w w  . ja v  a 2s. c o  m
    });
}

From source file:org.mzd.shap.spring.task.JobDaoSpringHibernate.java

License:Open Source License

public long countIncompleteTasks(final Integer jobId) {
    return getHibernateTemplate().execute(new HibernateCallback<Long>() {
        public Long doInHibernate(Session session) throws HibernateException, SQLException {
            return (Long) session.createCriteria(getPersistentClass()).add(Restrictions.idEq(jobId))
                    .setProjection(Projections.rowCount()).createCriteria("tasks")
                    .add(Restrictions.disjunction().add(Restrictions.eq("status", Status.NEW))
                            .add(Restrictions.eq("status", Status.QUEUED))
                            .add(Restrictions.eq("status", Status.STARTED)))
                    .uniqueResult();//www. j  a v  a 2s  .  co m
        }
    });
}

From source file:org.mzd.shap.spring.task.JobDaoSpringHibernate.java

License:Open Source License

public long countIncompleteTasks(final Job job) {
    return getHibernateTemplate().execute(new HibernateCallback<Long>() {
        public Long doInHibernate(Session session) throws HibernateException, SQLException {
            return (Long) session.createCriteria(getPersistentClass()).add(Restrictions.idEq(job.getId()))
                    .setProjection(Projections.rowCount()).createCriteria("tasks")
                    .add(Restrictions.disjunction().add(Restrictions.eq("status", Status.NEW))
                            .add(Restrictions.eq("status", Status.QUEUED))
                            .add(Restrictions.eq("status", Status.STARTED)))
                    .uniqueResult();/*from ww w .  j  a  va2s .c o  m*/
        }
    });
}

From source file:org.mzd.shap.spring.task.JobDaoSpringHibernate.java

License:Open Source License

public Job findUnfinishedById(final Integer jobId) {
    return getHibernateTemplate().execute(new HibernateCallback<Job>() {
        public Job doInHibernate(Session session) throws HibernateException, SQLException {
            return (Job) session.createCriteria(getPersistentClass()).add(Restrictions.idEq(jobId))
                    .add(Restrictions.conjunction().add(Restrictions.ne("status", Status.DONE))
                            .add(Restrictions.ne("status", Status.ERROR)))
                    .uniqueResult();// w  w  w.  j  ava2  s  .  c o  m
        }
    });
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private Criterion getConstraint2(GraphQuery query, ItemConstraint cons) {
    if (cons instanceof ItemGroupConstraint) {

        ItemGroupConstraint group = (ItemGroupConstraint) cons;
        Junction j = null;/*from   w  w w.ja  v a2  s  . c  om*/
        if (group.conjunction()) {
            j = Restrictions.conjunction();
        } else {
            j = Restrictions.disjunction();
        }

        for (Iterator iter = group.fetchConstraints().iterator(); iter.hasNext();) {
            ItemConstraint con = (ItemConstraint) iter.next();
            Criterion criterion = getConstraint2(query, con);
            if (group.conjunction()) {
                if (criterion == null) {
                    return (null);
                }
            }
            if (criterion != null) {
                j.add(criterion);
            }
        }
        return (j);
    }
    if (cons instanceof ItemNotConstraint) {
        ItemConstraint notCons = ((ItemNotConstraint) cons).fetchConstraint();
        if (notCons == null) {
            throw new IllegalArgumentException("ItemNotGroupConstraint must containt a constraint");
        }
        Criterion notCrit = getConstraint2(query, notCons);
        return (Expression.not(notCrit));
    }
    if (cons instanceof RelationConstraint) {
        RelationConstraint rel = (RelationConstraint) cons;
        DetachedCriteria crit = null;
        String direction = null;
        ItemSelector selector = query.findSelector(rel.getSelector());
        if (!(selector instanceof ItemRelationSelector)) {
            throw new IllegalArgumentException(
                    "RelationExpression selector " + rel.getSelector() + " is not a ItemRelationSelection!");
        }

        RelationExpression relExpr = new RelationExpression();

        if (selector instanceof RFCItemRelationSelector) {
            relExpr = new RFCRelationExpression();
        } else if (selector instanceof TransactionRelationSelector) {
            relExpr = new TransactionRelationExpression();
        }

        ItemRelationSelector relSelector = (ItemRelationSelector) selector;
        Criterion relation = null;
        if (rel.isTarget()) {
            if (relSelector.getSourceRange() != null) {
                if (relSelector.getSourceRange().size() == 0) {
                    log.info("RelationConstraint RelationSelector[" + relSelector.getId()
                            + "] SourceRange Empty!");

                    return (null);
                }
                relExpr.setSourceIds(relSelector.getSourceRange());
            } else {
                String srcId = ((ItemRelationSelector) selector).getSource();
                ItemSelector sel = (ItemSelector) query.findSelector(srcId);
                QueryExpression sExpr = getExpression(query, sel);
                if (sExpr.empty) {
                    return (null);
                }
                DetachedCriteria source = sExpr.criteria;
                relExpr.setSource(source);
            }
            relation = relExpr.getSourceCriterion();
            /*
            crit = relExpr.getSourceCriteria();
            relation = Property.forName("longId").in(crit.setProjection(Projections.property("targetId")));
            */
        } else {

            if (relSelector.getTargetRange() != null) {
                if (relSelector.getTargetRange().size() == 0) {
                    log.info("RelationConstraint RelationSelector[" + relSelector.getId()
                            + "] TargetRange Empty!");
                    return (null);
                }
                relExpr.setTargetIds(relSelector.getTargetRange());
                //relation = Property.forName("longId").in(relSelector.getTargetRange());
            } else {
                String trgId = ((ItemRelationSelector) selector).getTarget();
                ItemSelector sel = query.findSelector(trgId);
                QueryExpression tExpr = getExpression(query, sel);
                if (tExpr.empty) {
                    return (null);
                }
                DetachedCriteria target = tExpr.criteria;
                relExpr.setTarget(target);
            }
            relation = relExpr.getTargetCriterion();
            /*
            crit = relExpr.getTargetCriteria();
            relation = Property.forName("longId").in(crit.setProjection(Projections.property("sourceId")));
            */
        }
        log.debug(relation.toString());
        return (relation);
    }

    if (cons instanceof AttributeValueConstraint) {
        AttributeValueConstraint aValue = (AttributeValueConstraint) cons;
        AttributeValueExpression aExpr = new AttributeValueExpression();
        aExpr.setAlias(aValue.getAlias());
        aExpr.setOperation(aValue.getOperation());
        aExpr.setType(aValue.getValueType());
        aExpr.setStringValue(aValue.getValue());

        if (aExpr.isInternal()) {
            return (aExpr.getInternalCriterion());
        }

        DetachedCriteria attr = aExpr.getCriteria();
        return (Property.forName("longId").in(attr.setProjection(Projections.property("ownerId"))));
    }

    if (cons instanceof ItemSecurityConstraint) {
        ItemSecurityConstraint sCon = (ItemSecurityConstraint) cons;

        if (sCon.getGid() != null) {
            return (Property.forName("gid").eq(sCon.getGid()));
        }
        ItemExpression expr = new ItemExpression();
        expr.setAlias(sCon.getGroupName());

        DetachedCriteria gid = expr.getCriteria();
        return (Property.forName("gid").in(gid.setProjection(Projections.property("longId"))));
    }

    if (cons instanceof ItemIdConstraint) {
        ItemIdConstraint idContrain = (ItemIdConstraint) cons;
        if (idContrain.getId() != null) {
            return (Restrictions.idEq(idContrain.getId()));
        }
        if (idContrain.getAlias() != null) {
            return (Property.forName("alias").eq(idContrain.getAlias()));
        }
    }

    if (cons instanceof RFCTargetConstraint) {
        return (Property.forName("targetId").eq(((RFCTargetConstraint) cons).getLongId()));
    }

    if (cons instanceof AttributeSourceRelationConstraint) {
        AttributeSourceRelationConstraint relACons = (AttributeSourceRelationConstraint) cons;
        AttributeValueExpression expr = new AttributeValueExpression();
        expr.setAlias(relACons.getAlias());
        DetachedCriteria crit = expr.getCriteria();
        return (Property.forName("sourceId").in(crit.setProjection(Projections.property("valueAsLong"))));
    }
    log.error("Constraint{" + cons.getClass().getSimpleName() + "] not implemented!");
    return (null);
}

From source file:org.openmrs.module.metadatasharing.api.db.hibernate.HibernateMetadataDAO.java

License:Open Source License

@Override
public <T> T getItemById(Class<? extends T> type, String id) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(type);
    criteria.add(Restrictions.idEq(id));

    @SuppressWarnings("unchecked")
    T result = (T) criteria.uniqueResult();
    return result;
}