Example usage for org.hibernate.criterion Projections projectionList

List of usage examples for org.hibernate.criterion Projections projectionList

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections projectionList.

Prototype

public static ProjectionList projectionList() 

Source Link

Document

Create a new projection list.

Usage

From source file:com.jubination.model.dao.ProductsDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<String> fetchProductNames(String name) {
    session = getSessionFactory().getCurrentSession();
    return session.createCriteria(Products.class)
            .setProjection(Projections.projectionList().add(Projections.property("name")))
            .add(Restrictions.ilike("name", name, MatchMode.START))
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();

}

From source file:com.jubination.model.dao.ProductsDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<String> fetchCampaignNames(String name) {
    session = getSessionFactory().getCurrentSession();
    return session.createCriteria(Campaigns.class)
            .setProjection(Projections.projectionList().add(Projections.property("name")))
            .add(Restrictions.ilike("name", name, MatchMode.START))
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();
}

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

License:Open Source License

/**
 * Necessary to use the Projections to limit the selected columns to only
 * those defined to the Element tables (otherwise the returned columns
 * contains all columns for all associations).
 * /*from  w  w  w.  j  a va 2 s  .c  o  m*/
 * @see com.klistret.cmdb.dao.ElementDAO.findByCriteria
 * @return Collection
 */
public List<Element> find(List<String> expressions, int start, int limit) {
    try {
        logger.debug("Finding elements by expression from start position [{}] with limit [{}] for session [{}]",
                new Object[] { start, limit, getSession().hashCode() });

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

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

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

        criteria.addOrder(Order.asc("name"));

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

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

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

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

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

            elements.add(element);
        }

        results = null;

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

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

License:Open Source License

/**
 * Finds relations based on XPath expressions
 * //ww w .j a  v a 2s .c om
 */
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:com.kodemore.hibernate.criteria.KmRootCriteria.java

License:Open Source License

@Override
public void addProjection(Projection e) {
    if (_projections == null) {
        _projections = Projections.projectionList();
        getInnerCriteria().setProjection(_projections);
    }//from   www .  j  a v a  2 s  . c o  m

    _projections.add(e);
}

From source file:com.koylubaevnt.library.db.DataHelper.java

private DataHelper() {
    sessionFactory = HibernateUtil.getSessionFactory();

    bookProjectionList = Projections.projectionList();
    bookProjectionList.add(Projections.property("id"), "id");
    bookProjectionList.add(Projections.property("name"), "name");
    bookProjectionList.add(Projections.property("image"), "image");
    bookProjectionList.add(Projections.property("genre"), "genre");
    bookProjectionList.add(Projections.property("pageCount"), "pageCount");
    bookProjectionList.add(Projections.property("isbn"), "isbn");
    bookProjectionList.add(Projections.property("publisher"), "publisher");
    bookProjectionList.add(Projections.property("author"), "author");
    bookProjectionList.add(Projections.property("publishYear"), "publishYear");
    bookProjectionList.add(Projections.property("descr"), "descr");

    prepareCriterias();//from  w ww. ja va2 s .  co m
    runCountCriteria();

}

From source file:com.kunckle.jetpower.core.base.search.SearchAdapter.java

License:Apache License

/**
 * ??/*from  www .j  a va  2s  . c  o m*/
 *
 * @param projection Hibernate Criterion ?
 * @return 
 */

public SearchAdapter addProjection(Projection projection) {
    if (projectionList == null)
        projectionList = Projections.projectionList();
    projectionList.add(projection);
    return this;
}

From source file:com.library.Database.DataHelper.java

private Criteria getCriteriaWithProjection() {
    return getSession().createCriteria(Book.class).setProjection(Projections.projectionList()
            .add(Projections.property("id"), "id").add(Projections.property("isbn"), "isbn")
            .add(Projections.property("name"), "name").add(Projections.property("author"), "author")
            .add(Projections.property("genre"), "genre").add(Projections.property("publisher"), "publisher")
            .add(Projections.property("descr"), "descr").add(Projections.property("publishYear"), "publishYear")
            .add(Projections.property("pageCount"), "pageCount")).addOrder(Order.asc("name"));
}

From source file:com.liferay.portal.dao.orm.hibernate.ProjectionFactoryImpl.java

License:Open Source License

public ProjectionList projectionList() {
    return new ProjectionListImpl(Projections.projectionList());
}

From source file:com.liferay.portal.workflow.jbpm.dao.CustomSession.java

License:Open Source License

public List<ProcessDefinition> findProcessDefinitions(String name, boolean latest, int start, int end,
        OrderByComparator orderByComparator) {

    try {/*from  w  w w.j  av  a 2s.c  o  m*/
        Criteria criteria = _session.createCriteria(ProcessDefinition.class);

        if (latest) {
            ProjectionList projectionList = Projections.projectionList();

            projectionList.add(Projections.groupProperty("name"));
            projectionList.add(Projections.max("version"));

            criteria.setProjection(projectionList);

            addOrder(criteria, orderByComparator, "version");
        }

        if (name != null) {
            criteria.add(Restrictions.eq("name", name));

            addOrder(criteria, orderByComparator, "name");
        }

        if (latest == false && name == null) {
            addOrder(criteria, orderByComparator);
        }

        addPagination(criteria, start, end);

        if (latest) {
            List<Object[]> list = criteria.list();

            List<String> names = new ArrayList<String>(list.size());

            for (Object[] array : list) {
                names.add((String) array[0]);
            }

            return findProcessDefinitions(names);
        } else {
            return criteria.list();
        }
    } catch (Exception e) {
        throw new JbpmException(e);
    }
}