Java 类org.hibernate.criterion.PropertyProjection 实例源码

项目:shogun2    文件:GenericHibernateDao.java   
/**
 * Gets the results, that match a variable number of passed criterions, but return a
 * stripped version of the entities, where only the fieldNames in <code>restrictFieldNames</code>
 * have their actual values set.
 *
 * You can call this with <code>restrictFieldNames</code> = <code>null</code> to get the full
 * entities back.
 *
 * You can call this method without criterion arguments to find all entities (stripped down to
 * the <code>restrictFieldNames</code>).
 *
 * If this is called as <code>findByCriteriaRestricted(null)</code> the return value equals the
 * return value of <code>findByCriteria()</code>.
 *
 * @param restrictFieldNames
 * @param criterion
 * @return
 * @throws HibernateException
 */
@SuppressWarnings("unchecked")
public List<E> findByCriteriaRestricted(List<String> restrictFieldNames, Criterion... criterion) throws HibernateException {
    LOG.trace("Finding instances of " + entityClass.getSimpleName()
            + " based on " + criterion.length + " criteria");

    Criteria criteria = createDistinctRootEntityCriteria(criterion);

    if (restrictFieldNames != null){
        ProjectionList projectionList = Projections.projectionList();
        for (String restrictFieldName : restrictFieldNames) {
            PropertyProjection pp = Projections.property(restrictFieldName);
            projectionList.add(pp, restrictFieldName);
        }
        criteria.setProjection(projectionList);
        criteria.setResultTransformer(
            Transformers.aliasToBean(entityClass)
        );
    }

    return criteria.list();
}
项目:gocd    文件:PipelineStateDao.java   
public List<String> lockedPipelines() {
    return (List<String>) transactionTemplate.execute(new TransactionCallback() {
        @Override
        public Object doInTransaction(TransactionStatus status) {
            PropertyProjection pipelineName = Projections.property("pipelineName");
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add(
                    Restrictions.eq("locked", true));
            criteria.setCacheable(false);
            List<String> list = criteria.list();
            return list;
        }
    });
}