Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:ca.qc.bdeb.info.p58.starbuck.seriousentertainment.Application.CommandeDAO.java

public List<Commande> lireListeCommandes(int id) {
    List<Commande> list = null;
    try {//from w  ww. j  a v  a  2  s .  c  om
        Query query = session.createQuery("from Commande where ID_CLIENT = :id ");
        query.setParameter("id", id);
        list = query.list();
        session.getTransaction().commit();
    } catch (HibernateException e) {
        session.getTransaction().rollback();
    }
    return list;
}

From source file:ca.qc.bdeb.info.p58.starbuck.seriousentertainment.Application.CommandeJeuDAO.java

public List<CommandeJeu> lireListeDetailCommandes(int id) {
    List<CommandeJeu> list = null;
    try {//from  www. j av a 2s  . c  o  m
        Query query = session.createQuery("from CommandeJeu where ID_COMMANDE = :id ");
        query.setParameter("id", id);
        list = query.list();
        session.getTransaction().commit();
    } catch (HibernateException e) {
        session.getTransaction().rollback();
    }
    return list;
}

From source file:ca.qc.bdeb.info.p58.starbuck.seriousentertainment.Application.EmployeDAO.java

public Employe trouverEmployeDuClient(int id) {
    Employe emp = null;/*from  www  .ja v  a2 s .c  om*/
    try {
        Query query = session.createQuery("from Employe where ID_REPRESENTANT = :id ");
        query.setParameter("id", id);
        List<?> list = query.list();

        emp = (Employe) list.get(0);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        session.getTransaction().rollback();
    }
    return emp;
}

From source file:ca.ualberta.physics.cssdp.catalogue.dao.UrlDataProductDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<URI> findUrls(List<DataProduct> dataProducts, LocalDateTime start, LocalDateTime end) {

    List<URI> result;

    if (dataProducts != null && dataProducts.size() > 0) {

        String qlString = "select new java.net.URI(ud.url) from UrlDataProduct ud where ud.dataProduct in (:dataProducts) "
                + (start != null ? " and ud.startTimestamp <= :end " : "")
                + (end != null ? " and ud.endTimestamp >= :start" : "");

        javax.persistence.Query q = emp.get().createQuery(qlString);
        q.setParameter("dataProducts", dataProducts);
        if (start != null) {
            q.setParameter("start", start);
        }//  w  ww  .  j  a v a2 s  .  co  m
        if (end != null) {
            q.setParameter("end", end);
        }

        result = q.getResultList();

    } else {
        result = Lists.newArrayList();
    }

    return result;
}

From source file:cc.cnfc.core.orm.hibernate.HibernateDao.java

License:Open Source License

/**
 * ??HQL??/* ww w .  j  a v  a 2  s .  c  om*/
 *
 *  1?? a)?(MySQL,Oracle,SQL
 * Server2005)?limit n,m b)?(informix,Sybase 12.5.3,SQL
 * Server)?top n c)???? 2?
 * 3?order bygroup by,having  Query q= session.createQuery(
 * "select new A(M.a,N.b) from M as M,N as N where M.id=N.id");
 *
 * @param hql
 *            HQL??Where???select???? select
 *            a,b,(select c from table1) as d from table2 ...
 *            1)(select *)??from TUser
 *            2)select userName,password from TUser;
 * @param propertyNames
 *            ???
 * @param operators
 *            ????=???null
 *            ???=, >=, <=, <>, !=, like
 * @param values
 *            ?
 * @param offset
 *            0??-1
 * @param size
 *            ??-1
 * @param isTotalSize
 *            ??true
 * @param orderBy
 *            ?,??order byorderBy="a desc,b asc",??order by a
 *            desc,b asc
 * @param groupBy
 *            ,??group by groupBy="a desc,b asc",??group by a
 *            desc,b asc
 * @param otherCause
 *            where????(order by),(group by)??(having)
 * @param isCache
 *            ?cache "group by name order by name desc"
 * @return Object[] List list = (List)Object[0]
 *         int count = ((Integer)Object[1]).intValue;
 * @throws com.rwy.base.core.exception.GenericException
 * 
 */
public Object[] query(final String hql, final String[] propertyNames, final String[] operators,
        final Object[] values, final int offset, final int size, final boolean isTotalSize,
        final String orderBy, final String groupBy, final String otherCause, final boolean isCache)
        throws DataAccessException {
    Assert.hasText(hql, "hql?");

    Query query;
    String countSql;
    String fullSql;
    Integer count = 0;
    Map map = new HashMap();
    String where = "";

    if (propertyNames != null && propertyNames.length > 0 && values != null && values.length > 0) {
        if (propertyNames.length != values.length) {
            throw new GenericException();
        }

        if (operators != null && propertyNames.length != operators.length) {
            logger.error("");
            throw new GenericException();
        }

        for (int i = 0; i <= propertyNames.length - 1; i++) {
            if ("".equals(where)) {
                where = " where ";
            } else {
                where += "and ";
            }
            if (operators != null && operators[i].equalsIgnoreCase("isnull")) {
                where += propertyNames[i] + " is null ";
            } else if (operators != null && operators[i].equalsIgnoreCase("isnotnull")) {
                where += propertyNames[i] + " is not null ";
            } else if (operators != null && operators[i].equalsIgnoreCase("isempty")) {
                where += propertyNames[i] + " = '' ";
            } else if (operators != null && operators[i].equalsIgnoreCase("isnotempty")) {
                where += propertyNames[i] + " <> '' ";
            } else if (operators != null && operators[i].equalsIgnoreCase("in")) {
                where += propertyNames[i] + " in ";
            } else {
                where += propertyNames[i]
                        + (operators == null || operators[i] == null ? "=" : " " + operators[i]) + " ? ";
            }
        }

        fullSql = hql + where;
        fullSql += otherCause == null || otherCause.trim().equals("") ? "" : " " + otherCause;

        fullSql += groupBy == null || groupBy.trim().equals("") ? "" : " group by " + groupBy;
        fullSql += orderBy == null || orderBy.trim().equals("") ? "" : " order by " + orderBy;

        query = isCache ? this.createQuery(fullSql).setCacheable(true) : this.createQuery(fullSql);

        int paramIndex = 0;
        for (int i = 0; i <= values.length - 1; i++) {
            if (operators != null && operators[i].equalsIgnoreCase("isnull"))
                continue;
            if (operators != null && operators[i].equalsIgnoreCase("isnotnull"))
                continue;
            if (operators != null && operators[i].equalsIgnoreCase("isempty"))
                continue;
            if (operators != null && operators[i].equalsIgnoreCase("isnotempty"))
                continue;
            // IN?
            if (operators != null && operators[i].equalsIgnoreCase("in")) {
                values[i] = StringUtil.getInString(values[i]);
            }
            query.setParameter(paramIndex++, values[i]);
        }

    } else {
        if ("".equals(where) && hql.indexOf("where") == -1) {
            where = " where 1=1 ";
        } else if (!"".equals(where)) {
            where += " and ";
        }

        fullSql = hql + where;
        fullSql += otherCause == null || otherCause.trim().equals("") ? "" : " " + otherCause;

        fullSql += groupBy == null || groupBy.trim().equals("") ? "" : " group by " + groupBy;
        fullSql += orderBy == null || orderBy.trim().equals("") ? "" : " order by " + orderBy;

        query = this.createQuery(fullSql);
    }

    // ?
    if (isTotalSize) {

        // ???order by ???
        countSql = hql + where;

        // modi by hongdj

        countSql += groupBy == null || groupBy.trim().equals("") ? "" : " group by " + groupBy;
        countSql += otherCause == null || otherCause.trim().equals("") ? "" : " " + otherCause;

        // ?hql?hql??select????
        // select a,b,(select c from table1) as d from table2 ...
        countSql = "select count(*) from " + countSql.substring(countSql.toLowerCase().indexOf("from") + 5);
        Query query2 = this.createQuery(countSql);

        int paramIndex = 0;
        if (values != null) {
            for (int i = 0; i <= values.length - 1; i++) {
                if (operators != null && operators[i].equalsIgnoreCase("isnull"))
                    continue;
                if (operators != null && operators[i].equalsIgnoreCase("isnotnull"))
                    continue;
                if (operators != null && operators[i].equalsIgnoreCase("isempty"))
                    continue;
                if (operators != null && operators[i].equalsIgnoreCase("isnotempty"))
                    continue;
                query2.setParameter(paramIndex++, values[i]);
            }
        }
        // modi by denny
        Long res = (Long) query2.uniqueResult();
        if (res != null) {
            count = res.intValue();
        }
    }

    if (offset > 0) {
        query.setFirstResult(offset);
    }

    if (size > 0) {
        query.setMaxResults(size);
    }

    // ?log
    // logger.debug("fullSql=" + query.getQueryString());

    map.put("list", query.list());
    map.put("count", count);
    return new Object[] { map.get("list"), map.get("count") };
}

From source file:cc.cnfc.core.orm.hibernate.HibernateDao.java

License:Open Source License

public Query createQuery(final String queryString, final Object... values) {
    Assert.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }/*  w  w w. j a v a2 s  . c o m*/
    }
    return query;
}

From source file:cc.cnfc.core.orm.hibernate.SimpleHibernateDao.java

License:Open Source License

/**
 * ?HQL?Query./* ww  w  . j  a v  a2s.  co m*/
 * 
 * ?find()T,?T.
 * 
 * @param values
 *            ????,?.
 */
public Query createQuery(final String queryString, final Object... values) {
    Assert.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
    }
    return query;
}

From source file:celepsa.rrcc.da.ConsultasVariasDA.java

public boolean BuscarPersonaDocumento(String idDocPer) {

    boolean a = false;
    try {/*from  ww  w  .java 2 s  .co  m*/
        Query query = session.createQuery("FROM Tmdocumento d WHERE d.id = :pdid");
        query.setParameter("pdid", idDocPer);
        List<Tmdocumento> li = query.list();

        System.out.println(" lista " + li);
        a = li != null && li.size() > 0;
    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return a;
}

From source file:cgi.lemans.portail.domaine.gamaweb.impl.AbsenceDao.java

@Override
public Absence findAbsenceByPremierJourAbsence(String idRessource) {
    String hql = "from Absence a " + "where a.premierJourAbsence >= :dateToday "
            + "and a.refRessource.idRessource = :idRessource " + "order by a.premierJourAbsence";
    Query query = getSession().createQuery(hql);
    query.setParameter("idRessource", idRessource);
    query.setDate("dateToday", new java.util.Date());
    query.setMaxResults(1);/*from w w w  .ja va2s  . co m*/
    Absence results = (Absence) query.uniqueResult();
    return results;
}

From source file:cgi.lemans.portail.domaine.gamaweb.impl.AbsenceDao.java

@Override
public List<Absence> findAbsenceByEquipe(String equipeLibelle, String moisAafficher) {
    String hql = "select a from Absence a " + "left join a.refRessource ref " + "where ref.tags "
            + "like :equipeChoisie "
            //                   + "and (month(a.premierJourAbsence) = :moisAafficher or month(a.dateFinAbsence) = :moisAafficher) "
            + "and (year(a.premierJourAbsence) = :anneeEnCours or year(a.dateFinAbsence) = :anneeEnCours) "
            + "and ref.dateDepart > :date " + "order by ref.nom desc";
    Query query = getSession().createQuery(hql);
    query.setParameter("equipeChoisie", '%' + equipeLibelle + '%');
    query.setParameter("anneeEnCours", Calendar.getInstance().get(Calendar.YEAR));
    query.setDate("date", new java.util.Date());
    //        query.setParameter("moisAafficher", ConvertUtils.parseInteger(moisAafficher) + 1 );
    List<Absence> results = (List<Absence>) query.list();
    return results;
}