Example usage for org.hibernate.criterion ProjectionList add

List of usage examples for org.hibernate.criterion ProjectionList add

Introduction

In this page you can find the example usage for org.hibernate.criterion ProjectionList add.

Prototype

public ProjectionList add(Projection projection, String alias) 

Source Link

Document

Adds a projection to this list of projections after wrapping it with an alias

Usage

From source file:org.openmrs.module.dataintegrityworkflow.db.hibernate.HibernateDataIntegrityWorkflowDAO.java

License:Open Source License

public List getCheckRecordStatusCounts(IntegrityCheck integrityCheck) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(IntegrityCheckResult.class);
    criteria.add(Restrictions.eq("integrityCheck", integrityCheck));
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.groupProperty("status"), "statusId");
    projList.add(Projections.rowCount(), "count");
    criteria.setProjection(projList);//from   www.  j  a v  a2 s.  c o m
    return criteria.list();
}

From source file:org.shredzone.cilla.ws.assembler.CategoryAssembler.java

License:Open Source License

@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("title").as("title"));
    projection.add(Property.forName("icon").as("icon"));
    projection.add(Property.forName("caption.text").as("caption"));
    projection.add(Property.forName("caption.format").as("captionFormat"));
    return projection;
}

From source file:org.shredzone.cilla.ws.assembler.CommentAssembler.java

License:Open Source License

@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("mail").as("mail"));
    projection.add(Property.forName("url").as("url"));
    projection.add(Property.forName("text.text").as("text"));
    projection.add(Property.forName("text.format").as("textFormat"));
    projection.add(Property.forName("creation").as("creation"));
    projection.add(Property.forName("published").as("published"));
    projection.add(Property.forName("replyTo.id").as("replyToId"));
    projection.add(Property.forName("t.id").as("threadId"));
    projection.add(Property.forName("c.id").as("creatorId"));
    projection.add(Property.forName("c.login").as("creatorLogin"));
    projection.add(Property.forName("c.name").as("creatorName"));
    return projection;
}

From source file:org.shredzone.cilla.ws.assembler.HeaderAssembler.java

License:Open Source License

@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("caption").as("caption"));
    projection.add(Property.forName("description.text").as("description"));
    projection.add(Property.forName("description.format").as("descriptionFormat"));
    projection.add(Property.forName("creation").as("creation"));
    projection.add(Property.forName("enabled").as("enabled"));
    projection.add(Property.forName("location.longitude").as("longitude"));
    projection.add(Property.forName("location.latitude").as("latitude"));
    projection.add(Property.forName("location.altitude").as("altitude"));
    projection.add(Property.forName("t.commentable").as("commentable"));
    projection.add(Property.forName("c.id").as("creatorId"));
    projection.add(Property.forName("c.login").as("creatorLogin"));
    projection.add(Property.forName("c.name").as("creatorName"));
    return projection;
}

From source file:org.shredzone.cilla.ws.assembler.LanguageAssembler.java

License:Open Source License

@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("locale").as("locale"));
    return projection;
}

From source file:org.shredzone.cilla.ws.assembler.PageAssembler.java

License:Open Source License

/**
 * {@inheritDoc}//from   ww w . ja  va 2  s  . c om
 * <p>
 * Only creates a projection for {@link PageInfoDto}.
 */
@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("title").as("title"));
    projection.add(Property.forName("subject").as("subject"));
    projection.add(Property.forName("creation").as("creation"));
    projection.add(Property.forName("modification").as("modification"));
    projection.add(Property.forName("publication").as("publication"));
    projection.add(Property.forName("expiration").as("expiration"));
    projection.add(Property.forName("sticky").as("sticky"));
    projection.add(Property.forName("hidden").as("hidden"));
    projection.add(Property.forName("donatable").as("donatable"));
    projection.add(Property.forName("promoted").as("promoted"));
    projection.add(Property.forName("c.id").as("creatorId"));
    projection.add(Property.forName("c.login").as("creatorLogin"));
    projection.add(Property.forName("c.name").as("creatorName"));
    projection.add(Property.forName("t.commentable").as("commentable"));
    projection.add(Property.forName("language.id").as("languageId"));
    return projection;
}

From source file:org.shredzone.cilla.ws.assembler.UserAssembler.java

License:Open Source License

@Override
public ProjectionList projection() {
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.id(), "id");
    projection.add(Property.forName("login").as("login"));
    projection.add(Property.forName("name").as("name"));
    projection.add(Property.forName("mail").as("mail"));
    projection.add(Property.forName("timeZone").as("timeZone"));
    projection.add(Property.forName("r.name").as("roleName"));
    projection.add(Property.forName("l.id").as("languageId"));
    projection.add(Property.forName("l.locale").as("language"));
    return projection;
}

From source file:org.xerela.provider.tools.PluginProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
@SuppressWarnings({ "unchecked" })
public List<ToolRunDetails> getExecutionDetails(int executionId) {
    Session session = PluginsActivator.getSessionFactory().getCurrentSession();

    // Use projections to avoid loading Blob.  There is no lazy load on an
    // individual property in Hibernate -- only on collections one-to-X relationships.
    // The forums recommended this approach.  Note that there is a bug in Hibernate
    // that we are working around here -- but NOT MAPPING the executionId property
    // with an 'alias'.  Trying to do so seems to yield an invalid query from
    // Hibernate.  This means we have to set the executionId on the objects
    // manually after retrieval.  Gross.
    Criteria criteria = session.createCriteria(ToolRunDetails.class)
            .add(Restrictions.eq(EXECUTION_ID, executionId));
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("id"), "id") //$NON-NLS-1$ //$NON-NLS-2$
            .add(Projections.property(EXECUTION_ID)).add(Projections.property("device"), "device") //$NON-NLS-1$ //$NON-NLS-2$
            .add(Projections.property("error"), "error") //$NON-NLS-1$ //$NON-NLS-2$
            .add(Projections.property("gridData"), "gridData") //$NON-NLS-1$ //$NON-NLS-2$
            .add(Projections.property("startTime"), "startTime") //$NON-NLS-1$ //$NON-NLS-2$
            .add(Projections.property("endTime"), "endTime"); //$NON-NLS-1$ //$NON-NLS-2$
    criteria.setProjection(projList);/*from   ww  w.j a  va 2s  .  c o m*/
    criteria.setResultTransformer(Transformers.aliasToBean(ToolRunDetails.class));
    List<ToolRunDetails> list = criteria.list();

    for (ToolRunDetails details : list) {
        details.setExecutionId(executionId);
    }

    return list;
}

From source file:py.una.pol.karaku.dao.impl.BaseDAOImpl.java

License:Open Source License

@SuppressWarnings(UNCHECKED)
@Override//from w w w  .  j  a  v  a  2  s.  co m
@Nonnull
public List<T> get(Select select, Where<T> where, ISearchParam params) {

    if (!BaseEntity.class.isAssignableFrom(getClassOfT())) {
        throw new UnsupportedOperationException("Only works with baseEntity");
    }

    ISearchParam isp = params != null ? params : new SearchParam();
    isp.addOrder("id");
    Criteria criteria = getCriteria();
    Map<String, String> alias = new HashMap<String, String>();

    if (where != null && !where.isDistinct()) {
        log.debug("Making a not distinct query with Select, it's allways distinct");
    }

    if (select != null) {
        ProjectionList projections = Projections.projectionList();
        select.addAttribute("id");
        for (String projection : select.getAttributes()) {
            if (projection == null) {
                continue;
            }
            String property = BaseClauseHelper.configureAlias(criteria, projection, alias);

            projections.add(Projections.property(property), projection);

        }
        criteria.setProjection(projections);
        criteria.setResultTransformer(new KarakuAliasToBeanTransformer<T>(getClassOfT()));
    }

    generateWhere(where, alias, criteria);
    configureParams(isp, criteria, alias);

    List<T> toRet = criteria.list();
    if (toRet == null) {
        return new ArrayList<T>();
    }
    return toRet;
}

From source file:sncode.java.hibernate.dao.GenericDao.java

License:MIT License

/**
 * retrieve database data with given parameters
 * @param objName_      String with hibernate mapped object name
 * @param criterias_    List of hibernate criterion objects
 * @param orderbys_     List of hibernate order objects
 * @param first_        Integer with first row
 * @param max_          Integer with max rows in result List
 * @param projections_  List of hibernate projection objects
 * @param session_      Hibernate Session object
 * @return List of hbernate database mapped objects
 * @throws DaoException /*from w w  w .  j  a  v a 2s  .co  m*/
 */
public List retrieve(String objName_, List<Criterion> criterias_, List<Order> orderbys_, Integer first_,
        Integer max_, List<Projection> projections_, Session session_) throws DaoException {
    List data = null;
    Transaction tx = null;
    boolean globaltx = true;
    try {
        if (session_ == null) {
            session_ = this.startSession();
        }
        if (session_.getTransaction() == null || !session_.getTransaction().isActive()) {
            tx = session_.beginTransaction();
            globaltx = false;
        }

        Criteria c;
        c = session_.createCriteria(objName_);

        // add criterias
        if (criterias_ != null) {
            for (int i = 0; i < criterias_.size(); i++) {
                c.add(criterias_.get(i));
            }
        }

        // add projections
        if (projections_ != null) {
            ProjectionList pl = Projections.projectionList();
            for (int i = 0; i < projections_.size(); i++) {
                pl.add(projections_.get(i), "p_" + i);
            }
            c.setProjection(pl);
        }

        //add mas and first
        if (first_ != null) {
            c.setFirstResult(first_);
        }
        if (max_ != null) {
            c.setMaxResults(max_);
        }

        // add order
        if (orderbys_ != null) {
            for (int i = 0; i < orderbys_.size(); i++) {
                c.addOrder(orderbys_.get(i));
            }
        }

        data = c.list();

        if (!globaltx) {
            tx.commit();
        }

    } catch (Exception e) {
        logger.error("Error loading data : " + stacktraceError(e));
        throw new DaoException("Error loading data: " + stacktraceError(e));
    } finally {
        if (!globaltx) {
            if (session_ != null && session_.isOpen()) {
                if (tx != null && tx.isActive()) {
                    session_.flush();
                }
                session_.close();
                session_ = null;
            }
        }
    }

    return data;
}