Example usage for org.hibernate.criterion DetachedCriteria setProjection

List of usage examples for org.hibernate.criterion DetachedCriteria setProjection

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria setProjection.

Prototype

public DetachedCriteria setProjection(Projection projection) 

Source Link

Document

Set the projection to use.

Usage

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

License:Apache License

public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) {
    assert termsWithStats != null : "Unspecified terms.";
    assert vocabulary != null : "Unspecified vocabulary.";
    assert termsWithStats.size() == vocabulary.getTerms()
            .size() : "Sizes of terms to be filled and vocabulary terms don't match.";

    Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>(
            termsWithStats.size());//  w ww. j  a v  a 2 s  .  co  m
    for (VocabularyTermWithStats termWithStats : termsWithStats) {
        Long id = termWithStats.getTerm().getId();
        termsById.put(id, termWithStats);
    }

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // alias is the easiest way to restrict on association using criteria
    criteria.createAlias("vocabularyTerm", "term");
    criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("term.id"));
    criteria.setProjection(projectionList);

    final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria));

    for (Object[] result : results) {
        Integer numberOfUsages = (Integer) result[0];
        Long termId = (Long) result[1];
        termsById.get(termId).registerUsage(entityKind, numberOfUsages);
    }
}

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

License:Apache License

public boolean hasExternalData(SamplePE sample) throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(ExternalDataPE.class);
    criteria.add(Restrictions.eq("sampleInternal", sample));
    criteria.setProjection(Projections.rowCount());
    Integer count = (Integer) getHibernateTemplate().findByCriteria(criteria).get(0);
    return count > 0;
}

From source file:chiron.maxscore.dao.impl.BaseDAOImpl.java

/**
 * ??//from  ww  w  .ja v  a2  s. c  o m
 *
 * @param criteria ?
 * @return 
 */
@Override
public int count(DetachedCriteria criteria) {
    Session session = sessionFactory.getCurrentSession();
    return ((Number) criteria.setProjection(Projections.rowCount()).getExecutableCriteria(session)
            .uniqueResult()).intValue();
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

/**
 * /*  w  w w . j a v  a  2  s .  com*/
 * 
 * @param currentPage
 * @param clazz
 * @return
 */
public List<Object> getListByPage(int currentPage, DetachedCriteria detachedCriteria) {
    int pageSize = 100;
    // ??,?select * from xxx...
    detachedCriteria.setProjection(null);
    detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
    int firstResult = currentPage * pageSize;
    int maxResults = pageSize;
    List<Object> rows = this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, maxResults);
    return rows;
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

/**
 * //from   ww  w.  jav a2 s. c om
 * 
 * @param clazz
 * @return
 */
public int getTotalNum(DetachedCriteria detachedCriteria) {
    // ,hibernate?select count(id) from xxx....
    detachedCriteria.setProjection(Projections.rowCount());
    @SuppressWarnings("unchecked")
    List<Object> countList = this.getHibernateTemplate().findByCriteria(detachedCriteria);
    if (countList != null && countList.size() > 0) {
        int total = (Integer) countList.get(0);
        return total;
    } else
        return 0;
}

From source file:com.apress.progwt.server.dao.hibernate.SchoolDAOHibernateImpl.java

License:Apache License

/**
 * get the total number of rows without actually returning all rows
 * NOTE: important to set the start row here, otherwise when we start
 * paging, this criteria will be affected and we won't get the first
 * row.//from  w w  w  . ja v a2  s  .c  o m
 * 
 * @param criteria
 * @return
 */
private int getRowCount(DetachedCriteria criteria) {
    criteria.setProjection(Projections.rowCount());
    return ((Integer) getHibernateTemplate().findByCriteria(criteria, 0, 1).get(0)).intValue();
}

From source file:com.ateam.hibernate.HibernateDAOImpl.java

public String checkRole(String strUserName, String password) throws DataAccessException, java.sql.SQLException {
    String obj = null;/*ww  w  .  ja  v a  2s. co  m*/
    DetachedCriteria critthree = DetachedCriteria.forClass(UserAttr.class);
    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.groupProperty("userRole"));
    critthree.add(Restrictions.eq("userName", strUserName));
    critthree.add(Restrictions.eq("userPassword", password));
    critthree.setProjection(pl);
    List objs = getHibernateTemplate().findByCriteria(critthree);
    if ((objs != null) && (objs.size() > 0)) {
        obj = (String) objs.get(0);
    }
    return obj;
}

From source file:com.ateam.hibernate.HibernateDAOImpl.java

public String checkFullName(String strUserName, String password)
        throws DataAccessException, java.sql.SQLException {
    String obj = null;/* ww  w. j  a  v a 2s  .c  o  m*/
    DetachedCriteria critname = DetachedCriteria.forClass(UserAttr.class);
    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.groupProperty("userFullName"));
    critname.add(Restrictions.eq("userName", strUserName));
    critname.add(Restrictions.eq("userPassword", password));
    critname.setProjection(pl);
    List objs = getHibernateTemplate().findByCriteria(critname);
    if ((objs != null) && (objs.size() > 0)) {
        obj = (String) objs.get(0);
    }
    return obj;
}

From source file:com.ateam.hibernate.HibernateDAOImpl.java

public List<UserAttr> listUsers() throws DataAccessException, java.sql.SQLException {
    UserAttr obj = null;/*w w  w  . j  a  v  a  2  s  .  co m*/
    DetachedCriteria critfour = DetachedCriteria.forClass(UserAttr.class);
    critfour.setProjection(Projections.property("userName"));
    List objs = getHibernateTemplate().findByCriteria(critfour);

    return objs;
}

From source file:com.ateam.hibernate.HibernateDAOImpl.java

public List<Questions> listSkills() throws DataAccessException, java.sql.SQLException {
    Questions obj = null;/*from w  w  w  . jav a  2  s  .  c  om*/
    DetachedCriteria critfive = DetachedCriteria.forClass(Questions.class);
    critfive.setProjection(Projections.distinct(Projections.property("skillId")));
    List objs = getHibernateTemplate().findByCriteria(critfive);

    return objs;
}