Example usage for org.hibernate Criteria getAlias

List of usage examples for org.hibernate Criteria getAlias

Introduction

In this page you can find the example usage for org.hibernate Criteria getAlias.

Prototype

public String getAlias();

Source Link

Document

Get the alias of the entity encapsulated by this criteria instance.

Usage

From source file:com.amalto.core.storage.hibernate.StandardQueryHandler.java

License:Open Source License

@SuppressWarnings("rawtypes")
public static Criteria findCriteria(Criteria mainCriteria, Set<String> aliases) {
    if (aliases.contains(mainCriteria.getAlias())) {
        return mainCriteria;
    }//from ww  w.  ja va 2  s  .  c  o  m
    if (mainCriteria instanceof CriteriaImpl) {
        Criteria foundSubCriteria = null;
        Iterator iterator = ((CriteriaImpl) mainCriteria).iterateSubcriteria();
        while (iterator.hasNext()) {
            Criteria subCriteria = (Criteria) iterator.next();
            if (aliases.contains(subCriteria.getAlias())) {
                foundSubCriteria = subCriteria;
                break;
            }
        }
        if (foundSubCriteria == null) {
            throw new IllegalStateException("Could not find criteria for type check."); //$NON-NLS-1$
        }
        return foundSubCriteria;
    } else {
        throw new IllegalStateException(
                "Expected a criteria instance of " + CriteriaImpl.class.getName() + "."); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:com.evolveum.midpoint.repo.sql.query.restriction.ItemRestriction.java

License:Apache License

protected void addNewCriteriaToContext(ItemPath path, Definition def, String realName) {
    ItemPath lastPropPath = path.allExceptLast();
    if (ItemPath.EMPTY_PATH.equals(lastPropPath)) {
        lastPropPath = null;//from   w  ww.  j a  v a2s.  c  o  m
    }

    // Virtual path is defined for example for virtual collections. {c:role/c:assignment} and {c:role/c:iducement}
    // must use the same criteria, therefore {c:role/assigmnents} is also path under which is this criteria saved.
    final ItemPath virtualPath = lastPropPath != null ? new ItemPath(lastPropPath, new QName("", realName))
            : new ItemPath(new QName("", realName));

    Criteria existing = getContext().getCriteria(path);
    if (existing != null) {
        return;
    }

    // If there is already criteria on virtual path, only add new path to aliases and criterias.
    Criteria virtualCriteria = getContext().getCriteria(virtualPath);
    if (virtualCriteria != null) {
        getContext().addAlias(path, virtualCriteria.getAlias());
        getContext().addCriteria(path, virtualCriteria);
        return;
    }

    // get parent criteria
    Criteria pCriteria = getContext().getCriteria(lastPropPath);

    // create new criteria and alias for this relationship
    String alias = getContext().addAlias(path, def);
    Criteria criteria = pCriteria.createCriteria(realName, alias, JoinType.LEFT_OUTER_JOIN);
    getContext().addCriteria(path, criteria);
    //also add virtual path to criteria map
    getContext().addCriteria(virtualPath, criteria);
}

From source file:com.klistret.cmdb.dao.RelationDAOImpl.java

License:Open Source License

/**
 * Finds relations based on XPath expressions
 * /* ww w  . j a  v a2  s  .  co  m*/
 */
public List<Relation> find(List<String> expressions, int start, int limit) {
    try {
        logger.debug("Finding relations by expression from start position [{}] with limit [{}]", start, limit);

        if (expressions == null)
            throw new ApplicationException("Expressions parameter is null", new IllegalArgumentException());

        Criteria criteria = new XPathCriteria(expressions, getSession()).getCriteria();
        String alias = criteria.getAlias();

        criteria.setProjection(Projections.projectionList().add(Projections.property(alias + ".id"))
                .add(Projections.property(alias + ".type")).add(Projections.property(alias + ".source"))
                .add(Projections.property(alias + ".destination"))
                .add(Projections.property(alias + ".fromTimeStamp"))
                .add(Projections.property(alias + ".toTimeStamp"))
                .add(Projections.property(alias + ".createId"))
                .add(Projections.property(alias + ".createTimeStamp"))
                .add(Projections.property(alias + ".updateTimeStamp"))
                .add(Projections.property(alias + ".version"))
                .add(Projections.property(alias + ".configuration")));

        criteria.setFirstResult(start);
        criteria.setMaxResults(limit);
        criteria.setFetchSize(limit);

        Object[] results = criteria.list().toArray();

        List<Relation> relations = new ArrayList<Relation>(results.length);
        logger.debug("Results length [{}]", results.length);

        for (int index = 0; index < results.length; index++) {
            Object[] row = (Object[]) results[index];

            Relation relation = new Relation();
            relation.setId((Long) row[0]);
            relation.setType((RelationType) row[1]);
            relation.setSource((Element) row[2]);
            relation.setDestination((Element) row[3]);
            relation.setFromTimeStamp((Date) row[4]);
            relation.setToTimeStamp((Date) row[5]);
            relation.setCreateId((String) row[6]);
            relation.setCreateTimeStamp((Date) row[7]);
            relation.setUpdateTimeStamp((Date) row[8]);
            relation.setVersion((Long) row[9]);
            relation.setConfiguration((com.klistret.cmdb.ci.commons.Relation) row[10]);

            relations.add(relation);
        }

        results = null;

        return relations;
    } catch (StaleStateException e) {
        throw new ApplicationException(
                "Relation(s) found are stale which means newer version exists (Hibernate).");
    } catch (HibernateException e) {
        throw new InfrastructureException(e.getMessage(), e);
    }
}

From source file:de.decidr.model.filters.StartableWorkflowModelFilter.java

License:Apache License

/**
 * {@inheritDoc}/* w  ww  . j  a  v  a  2 s . c om*/
 */
public void apply(Criteria criteria) {
    DetachedCriteria exists = DetachedCriteria.forClass(StartableWorkflowModelView.class, "startableFilter");
    exists.add(Restrictions.eqProperty(exists.getAlias() + ".id", criteria.getAlias() + ".id"));
    /*
     * Workaround for Hibernate issue HHH-993: Criteria subquery without
     * projection fails throwing NullPointerException.
     * 
     * Additionally, Mysql doesn't seem to like aliases in EXISTS
     * subqueries, so we have to explicitly specify "*"
     */
    exists.setProjection(Projections.sqlProjection("*", new String[0], new Type[0]));
    criteria.add(Subqueries.exists(exists));
}

From source file:org.broadleafcommerce.cms.common.AbstractContentService.java

License:Apache License

private <T> void addSandboxCriteria(SandBox sandbox, Criteria c, Class<T> type, String originalIdProperty) {
    Criterion originalSandboxExpression = Restrictions.eq("originalSandBox", sandbox);
    Criterion currentSandboxExpression = Restrictions.eq("sandbox", sandbox);
    Criterion userSandboxExpression = Restrictions.or(currentSandboxExpression, originalSandboxExpression);
    Criterion productionSandboxExpression = null;
    if (sandbox.getSite() == null || sandbox.getSite().getProductionSandbox() == null) {
        productionSandboxExpression = Restrictions.isNull("sandbox");
    } else {/*from www .j ava 2s  .co  m*/
        productionSandboxExpression = Restrictions.eq("sandbox", sandbox.getSite().getProductionSandbox());
    }

    if (productionSandboxExpression != null) {
        c.add(Restrictions.or(userSandboxExpression, productionSandboxExpression));
    } else {
        c.add(userSandboxExpression);
    }

    // Build a sub-query to exclude items from production that are also in my sandbox.
    // (e.g. my sandbox always wins even if the items in my sandbox don't match the
    // current criteria.)
    //
    // This subquery prevents the following:
    // 1.  Duplicate items (one for sbox, one for prod)
    // 2.  Filter issues where the production item qualifies for the passed in criteria
    //     but has been modified so that the item in the sandbox no longer does.
    // 3.  Inverse of #2.
    DetachedCriteria existsInSboxCriteria = DetachedCriteria.forClass(type, "sboxItem");
    existsInSboxCriteria.add(userSandboxExpression);
    existsInSboxCriteria.add(Restrictions.eq("archivedFlag", false));
    String outerAlias = c.getAlias();
    existsInSboxCriteria.add(Property.forName(outerAlias + ".id").eqProperty("sboxItem." + originalIdProperty));
    existsInSboxCriteria.setProjection(Projections.id());
    c.add(Subqueries.notExists(existsInSboxCriteria));
}