Example usage for org.hibernate.criterion Expression sql

List of usage examples for org.hibernate.criterion Expression sql

Introduction

In this page you can find the example usage for org.hibernate.criterion Expression sql.

Prototype

@Deprecated
public static Criterion sql(String sql) 

Source Link

Document

Apply a constraint expressed in SQL with no parameters.

Usage

From source file:com.ihsolution.hqipo.dao.utils.QueryHelper.java

License:Open Source License

/**
 * convert the InputDTO to QueryHelper to get the count (omitting sort order)
 *///  www . j ava 2s  .  c o  m
@SuppressWarnings("deprecation")
public QueryHelper convertDtoToQhelperForCount(InputDTO dto) {
    if (dto == null)
        return this;
    this.dto = dto;
    int ind = 0;
    try {
        Junction j = null;
        Junction conj = Restrictions.conjunction();
        Junction disj = Restrictions.disjunction();
        String operator = "";
        boolean disjB = false, conjB = false;
        if (dto.getFetchMode() != null) {
            for (Map.Entry<String, String> entry : dto.getFetchMode().entrySet()) {
                FetchMode fmode = null;
                logger.debug("FetchMode key=" + entry.getKey() + " val=" + entry.getValue());
                if (entry.getValue().equals("join"))
                    fmode = FetchMode.JOIN;
                else if (entry.getValue().equals("eager"))
                    fmode = FetchMode.EAGER;
                else if (entry.getValue().equals("lazy"))
                    fmode = FetchMode.LAZY;
                else
                    fmode = FetchMode.LAZY;
                this.detCriteria.setFetchMode(entry.getKey(), fmode);
            }
        }
        for (String field : dto.getFields()) {
            operator = dto.getOperators().get(ind);
            if ("or".equals(operator)) {
                j = disj;
                disjB = true;
            } else {
                j = conj;
                conjB = true;
            }
            this.addFieldAndVal(createAliases(field), dto.getValues().get(ind), dto.getOperations().get(ind),
                    j);
            ind++;
        }

        if (dto.getExpressions() != null) {
            for (String expr : dto.getExpressions()) {
                j.add(Expression.sql(expr));
            }
        }
        if (dto.getFieldsToSelect() != null && dto.getFieldsToSelect().length > 0) {
            ProjectionList prList = Projections.projectionList();
            Projection projection = null;

            for (String fld : dto.getFieldsToSelect()) {
                String als = this.createAliases(fld);
                prList.add(Projections.property(als));
            }
            if (dto.isDistinct()) {
                projection = Projections.distinct(prList);
            } else {
                projection = prList;
            }
            this.detCriteria.setProjection(projection);

        } else {
            this.fldSelectedSet = false;
        }

        if (disjB)
            detCriteria.add(disj);
        if (conjB)
            detCriteria.add(conj);
        if (logger.isDebugEnabled()) {
            if (conjB)
                logger.debug("conjuction=" + conj.toString());
            if (disjB)
                logger.debug("disjunction=" + disj.toString());
        }
        if (dto.isDistinct())
            detCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e);
    }
    return this;
}

From source file:eu.ist_phosphorus.harmony.idb.database.hibernate.Reservation.java

License:Open Source License

/**
 * Get reservations summarized under one job-Id.
 * /*from  w w  w  . j  a v  a 2 s.  com*/
 * @param jobId
 *            Id which summarizes reservations
 * @return List of Reservations summarized under the committed job-Id
 * @throws DatabaseException
 */
@SuppressWarnings("unchecked")
public static final List<Reservation> loadJob(final long jobId) throws DatabaseException {
    return (List<Reservation>) (new TransactionManager("jobID=" + jobId) {
        @Override
        protected void dbOperation() {
            this.result = this.session.createCriteria(Reservation.class).setFetchMode("", FetchMode.SELECT)
                    .add(Expression.sql((String) this.arg)).list();
        }
    }).getResult();
}