Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.MaterialDAO.java

License:Apache License

public List<MaterialPE> listMaterialsWithPropertiesAndInhibitor(final MaterialTypePE materialType)
        throws DataAccessException {
    assert materialType != null : "Unspecified material type.";

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("materialType", materialType));
    final int count = DAOUtils.getCount(criteria);
    if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES) {
        criteria.setFetchMode("materialProperties", FetchMode.JOIN);
    } else {//  w ww . ja v a2s . c  o  m
        operationLog.info(String.format("Found %d materials, disable properties loading.", count));
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<MaterialPE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%d materials have been found for material type '%s'.", list.size(),
                materialType));
    }
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.PropertyTypeDAO.java

License:Apache License

public List<PropertyTypePE> listAllPropertyTypes() {
    final Criteria criteria = getSession().createCriteria(PropertyTypePE.class);
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<PropertyTypePE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(): %d property types(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), list.size()));
    }/*  ww w .j a v a  2  s .  c om*/
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.PropertyTypeDAO.java

License:Apache License

public List<PropertyTypePE> listAllPropertyTypesWithRelations() {
    final Criteria criteria = getSession().createCriteria(PropertyTypePE.class);
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    criteria.setFetchMode("materialTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("sampleTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("experimentTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("dataSetTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<PropertyTypePE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(): %d property types(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), list.size()));
    }//from w w  w  .  j a va 2  s  .  co m
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.SampleDAO.java

License:Apache License

private List<SamplePE> listSamplesByCriteria(final Criteria basicCriteria, boolean withExperimentAndProperties,
        Criterion... additionalCriterions) throws DataAccessException {
    for (Criterion criterion : additionalCriterions) {
        basicCriteria.add(criterion);//from  w w w  .  j  a  va2s  . c  om
    }
    final int count = DAOUtils.getCount(basicCriteria);
    if (withExperimentAndProperties) {
        basicCriteria.setFetchMode("experimentInternal", FetchMode.JOIN);
        if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES) {
            basicCriteria.setFetchMode("sampleProperties", FetchMode.JOIN);
        } else {
            operationLog.info(String.format("Found %d samples, disable properties loading.", count));
        }
    }
    basicCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return cast(basicCriteria.list());
}

From source file:classes.PurchaseOrderAction.java

License:Apache License

public String find() throws Exception {
    logger.info("Starting find()"); //f:log
    Session sess = HibernateUtil.getSessionFactory().openSession(); //f:hibernate
    Transaction t = sess.beginTransaction(); //f:hibernate

    Criteria criteria = sess.createCriteria(PurchaseOrder.class); //f:hibernate

    criteria.add(//from ww w.  jav  a2 s.  co  m
            Example.create(this.purchaseOrder).excludeZeroes().ignoreCase().enableLike(MatchMode.ANYWHERE)); //f:hibernate
    if (this.purchaseOrder.getId() != null) { //f:hibernate
        criteria.add(Restrictions.idEq(this.purchaseOrder.getId())); //f:hibernate
    } //f:hibernate

    if (this.purchaseOrder.getCustomer().getId() != null) { //f:hibernate
        criteria.add(Restrictions.eq("customer", this.purchaseOrder.getCustomer())); //f:hibernate
    } //f:hibernate

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); //f:hibernate
    @SuppressWarnings("unchecked")
    List<PurchaseOrder> l = (List<PurchaseOrder>) criteria.list(); //f:hibernate
    request.put("list", l);
    t.commit(); //f:hibernate
    sess.close(); //f:hibernate

    this.task = SystemConstants.CR_MODE;
    logger.info("Finishing input()"); //f:log
    return INPUT;
}

From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria./*from  ww w .j a v  a  2  s  .c om*/
 */
protected long countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    // Count
    Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    long totalCount = (totalCountObject != null) ? totalCountObject : 0;

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    return totalCount;
}

From source file:cn.newtouch.util.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria./*from w w w.j  a  v a 2 s  .  com*/
 */
@SuppressWarnings("unchecked")
protected long countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    // Count
    Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    long totalCount = (totalCountObject != null) ? totalCountObject : 0;

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    return totalCount;
}

From source file:cn.trymore.core.dao.impl.DAOGenericImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public PaginationSupport<T> findPageByCriteria(final DetachedCriteria criteria, final int pageSize,
        final int startIndex, final boolean dataFilter) throws DAOException {
    if (dataFilter && UtilString.isNotEmpty(this.getQueryFilter())) {
        criteria.add(Restrictions.sqlRestriction(this.getQueryFilter()));
        this.setQueryFilter(null);
    }// w ww.j  a  va2 s  .com

    return (PaginationSupport<T>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria execCriteria = criteria.getExecutableCriteria(session);

            int rowCount = ((Integer) execCriteria.setProjection(Projections.rowCount()).uniqueResult())
                    .intValue();
            execCriteria.setProjection(null);
            execCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            //execCriteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
            execCriteria.setFirstResult(startIndex);

            if (pageSize > 0) {
                execCriteria.setMaxResults(pageSize);
            } else {
                execCriteria.setMaxResults(rowCount);
            }
            List<T> items = execCriteria.list();
            return rowCount > 0
                    ? new PaginationSupport<T>(items, rowCount, startIndex, pageSize > 0 ? pageSize : rowCount)
                    : null;
        }
    });
}

From source file:com.abiquo.server.core.enterprise.UserDAO.java

License:Open Source License

private Criteria createCriteria(final Enterprise enterprise, final Role role, final String[] filters,
        final String orderBy, final boolean desc, final boolean connected) {
    Criteria criteria = createCriteria();

    if (enterprise != null) {
        criteria.add(sameEnterprise(enterprise));
    }/*from ww w  .j av  a2s.c  o  m*/

    if (role != null) {
        criteria.add(sameRole(role));
    }
    if (!ArrayUtils.isEmpty(filters)) {
        for (String filter : filters) {
            if (!StringUtils.isEmpty(filter)) {
                criteria.add(filterBy(filter));
            }
        }
    }

    if (!StringUtils.isEmpty(orderBy)) {
        Order order = Order.asc(orderBy);
        if (desc) {
            order = Order.desc(orderBy);
        }
        criteria.addOrder(order);
    }

    if (connected) {
        criteria.createCriteria("sessions").add(Restrictions.gt("expireDate", new Date()));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    }
    return criteria;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * countCriteria.//from w  w  w .  j  a va2  s  . co m
 */
@SuppressWarnings("unchecked")
protected int countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("can not throw Exception:{}", e.getMessage());
    }

    // Count
    Long ct = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    int totalCount = ct == null ? 0 : ct.intValue();

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("can not throw Exception:{}", e.getMessage());
    }

    return totalCount;
}