Example usage for org.hibernate Query setProperties

List of usage examples for org.hibernate Query setProperties

Introduction

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

Prototype

Query<R> setProperties(Map bean);

Source Link

Document

Bind the values of the given Map for each named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.

Usage

From source file:com.lighting.platform.base.dao.SimpleHibernateDao.java

License:Apache License

/**
 * ?HQL?Query./* w  ww  . j ava 2 s.c o m*/
 * find()???.
 * 
 * @param values ???,??.
 */
public Query createQuery(final String queryString, final Map<String, ?> values) {
    AssertUtils.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null) {
        query.setProperties(values);
    }
    return query.setCacheable(cacheable());
}

From source file:com.mg.framework.api.orm.OrmTemplate.java

License:Open Source License

/**
 * ?  EJBQL ?   /*from  w  w  w . ja  v a2s.  com*/
 *
 * @param queryString ? ?
 * @param params       ?  - 
 * @return  ?
 */
public List findByNamedParam(final String queryString, final Map<String, Object> params) {
    return (List) execute(new HibernateCallback<List>() {
        public List doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery(queryString);
            prepareQuery(queryObject);
            queryObject.setProperties(params);
            return queryObject.list();
        }
    });
}

From source file:com.mg.framework.api.orm.OrmTemplate.java

License:Open Source License

/**
 * ?   EJBQL ?   /*from  w w w . j  a va 2  s  .c  o m*/
 *
 * @param queryName ? ?
 * @param params     ?  - 
 * @return  ?
 */
public List findByNamedQueryAndNamedParam(final String queryName, final Map<String, Object> params) {
    return (List) execute(new HibernateCallback<List>() {
        public List doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            prepareQuery(queryObject);
            queryObject.setProperties(params);
            return queryObject.list();
        }
    });
}

From source file:com.project.framework.dao.GenericDao.java

License:Apache License

/**
 * ???Query.// w w w  . j  a  v a  2  s.  co  m
 * 
 * @param values ???,??.
 */
public Query createQuery(final String queryString, final Map<String, ?> values) {

    Assert.hasText(queryString, "queryString Can not NULL");

    Query query = getSession().createQuery(queryString);
    if (values != null) {
        query.setProperties(values);
    }
    return query;
}

From source file:com.qfix.vms.dao.impl.UserDaoImpl.java

@Override
public User login(String email, String password) {
    try {//w  ww  .ja v  a  2s.c o m
        password = getSHA(password);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    Query query = getSession()
            .createSQLQuery("select * from system_user where email=:email and password=:password");
    query.setProperties(User.class);
    query.setString("email", email);
    query.setString("password", password);
    List list = query.list();
    User user = null;
    for (Iterator it = list.iterator(); it.hasNext();) {
        User u = (User) it.next();
        user = new User(u.getUsername(), u.getEmail(), u.getPassword(), u.getUserType());

    }
    return user;

}

From source file:com.rosy.bill.dao.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * ?SQL?Query./*from w w  w.  ja v  a 2 s  .c o  m*/
 * find()???.
 * 
 * @param values ???,??.
 */
public Query createSqlQuery(final String queryString, final Map<String, ?> values) {

    Assert.hasText(queryString, "queryString?");
    Query query = getSession().createSQLQuery(queryString);

    if (values != null) {
        query.setProperties(values);
    }
    return query;
}

From source file:com.square.core.dao.implementations.ActionDaoImplementation.java

License:Open Source License

private Query creerQuery(String select, RemotePagingCriteriasDto<ActionCritereRechercheDto> criteres,
        boolean addOrderBy) {
    final StringBuffer requete = new StringBuffer(select);
    requete.append(" from Action act");
    requete.append(" left join act.sousObjet ssObjet");
    requete.append(" inner join act.statut statutAction");
    requete.append(" inner join act.actionAttribution attribution");
    requete.append(" inner join act.ressource createur");
    requete.append(" left join attribution.ressource attributionRessource");
    requete.append(" left join attribution.agence attributionAgence");
    requete.append(" inner join act.actionAffectation affectation");
    requete.append(" left join affectation.personne affectationPersonne");
    requete.append(" left join affectation.opportunite affectationOpportunite");
    requete.append(" left join act.documents document");
    requete.append(" where 1=1");

    final ActionCritereRechercheDto actionCritereDto = criteres.getCriterias();

    final Map<String, Object> parameters = new HashMap<String, Object>();
    if (actionCritereDto != null) {
        final List<Long> idsAgence = actionCritereDto.getIdAgences();
        final List<Long> idsCommerciaux = actionCritereDto.getIdCommerciaux();
        final boolean hasCritereCommercial = idsCommerciaux != null && !idsCommerciaux.isEmpty();
        final boolean hasCritereAgence = idsAgence != null && !idsAgence.isEmpty();

        if (actionCritereDto.getIdsActions() != null && !actionCritereDto.getIdsActions().isEmpty()) {
            requete.append(" and act.id in (:idsActions)");
            parameters.put("idsActions", actionCritereDto.getIdsActions());
        }/*from   w w  w .  ja  v a 2s . c o m*/
        if (!Boolean.TRUE.equals(actionCritereDto.getDesactiverFiltreAffichage())) {
            requete.append(" and (act.dateAffichage is null or act.dateAffichage <= NOW()) ");
        }
        // Critre sur le type de l'action.
        if (actionCritereDto.getIdType() != null) {
            requete.append(" and act.type.id = :idType");
            parameters.put("idType", actionCritereDto.getIdType());
        }
        // Critre sur la nature de l'action.
        if (actionCritereDto.getListeNatureActions() != null
                && !actionCritereDto.getListeNatureActions().isEmpty()) {
            requete.append(" and act.nature.id in (:idsNatureAction)");
            parameters.put("idsNatureAction", actionCritereDto.getListeNatureActions());
        }
        // Critre sur la nature du rsultat.
        if (actionCritereDto.getListeNatureResultats() != null
                && !actionCritereDto.getListeNatureResultats().isEmpty()) {
            requete.append(" and act.natureResultat.id in (:idsNatureResultats)");
            parameters.put("idsNatureResultats", actionCritereDto.getListeNatureResultats());
        }
        // Critre sur la priorit de l'action.
        if (actionCritereDto.getListePriorites() != null && !actionCritereDto.getListePriorites().isEmpty()) {
            requete.append(" and act.priorite.id in (:idsPriorites)");
            parameters.put("idsPriorites", actionCritereDto.getListePriorites());
        }
        // Critre sur le rsultat de l'action.
        if (actionCritereDto.getListeResultats() != null && !actionCritereDto.getListeResultats().isEmpty()) {
            requete.append(" and act.resultat.id in (:idsResultats)");
            parameters.put("idsResultats", actionCritereDto.getListeResultats());
        }
        // Critre sur le statut de l'action.
        if (actionCritereDto.getListeStatuts() != null && !actionCritereDto.getListeStatuts().isEmpty()) {
            requete.append(" and statutAction.id in (:idsStatuts)");
            parameters.put("idsStatuts", actionCritereDto.getListeStatuts());
        }
        // Critre sur l'objet de l'action.
        if (actionCritereDto.getIdObjet() != null) {
            requete.append(" and act.objet.id = :idObjet");
            parameters.put("idObjet", actionCritereDto.getIdObjet());
        }
        // Critre sur le sous objet de l'action.
        if (actionCritereDto.getIdSousObjet() != null) {
            requete.append(" and ssObjet.id = :idSsObjet");
            parameters.put("idSsObjet", actionCritereDto.getIdSousObjet());
        }
        // Critre sur la campagne.
        if (actionCritereDto.getListeCampagnes() != null && !actionCritereDto.getListeCampagnes().isEmpty()) {
            requete.append(" and act.campagne.id in (:idsCampagnes)");
            parameters.put("idsCampagnes", actionCritereDto.getListeCampagnes());
        }
        // Critre sur le crateur de l'action.
        final List<Long> idsCreateur = actionCritereDto.getIdCreateurs();
        if (idsCreateur != null && !idsCreateur.isEmpty()) {
            requete.append(" and act.ressource.id in (:idsCreateur)");
            parameters.put("idsCreateur", idsCreateur);
        }
        // Critre sur la ressource ou sur l'agence responsable de l'action.
        if (hasCritereCommercial && hasCritereAgence) {
            // Si recherche de type ET ou non renseign : recherche de type ET
            if (actionCritereDto.getRechercheEtEntreAgencesEtCommerciaux() == null
                    || Boolean.TRUE.equals(actionCritereDto.getRechercheEtEntreAgencesEtCommerciaux())) {
                requete.append(
                        " and attributionRessource.id in (:idsCommerciaux) and attributionAgence.id in (:idsAgence)");
            } else {
                // Sinon recherche de type OU
                requete.append(
                        " and (attributionRessource.id in (:idsCommerciaux) or attributionAgence.id in (:idsAgence))");
            }
            parameters.put("idsCommerciaux", idsCommerciaux);
            parameters.put("idsAgence", idsAgence);
        } else {
            if (hasCritereCommercial) {
                requete.append(" and attributionRessource.id in (:idsCommerciaux)");
                parameters.put("idsCommerciaux", idsCommerciaux);
            }
            if (hasCritereAgence) {
                requete.append("");
                // Si recherche de type ET : recherche des actions de l'agence sans affectation  un commercial
                if (actionCritereDto.getRechercheEtEntreAgencesEtCommerciaux() == null
                        || Boolean.TRUE.equals(actionCritereDto.getRechercheEtEntreAgencesEtCommerciaux())) {
                    requete.append(
                            " and attributionAgence.id in (:idsAgence) and attributionRessource is null");
                } else {
                    // Sinon recherche de type OU : recherche de l'ensemble des actions de l'agence (avec ou sans affectation  un commercial)
                    requete.append(" and attributionAgence.id in (:idsAgence)");
                }
                parameters.put("idsAgence", idsAgence);
            }
        }
        // Identifiants des agences  laquelle l'action est attribue  exclure de la recherche
        final Set<Long> idsAgencesExclues = actionCritereDto.getIdsAgencesExclues();
        if (idsAgencesExclues != null && !idsAgencesExclues.isEmpty()) {
            requete.append(" and attributionAgence.id not in (:idsAgencesExclues)");
            parameters.put("idsAgencesExclues", idsAgencesExclues);
        }
        // Critre sur la date dbut de l'action.
        if (actionCritereDto.getDateDebutAction() != null) {
            requete.append(" and act.date >= :dateDebutAction");
            parameters.put("dateDebutAction", actionCritereDto.getDateDebutAction());
        }
        if (actionCritereDto.getDateNotification() != null) {
            requete.append(" and act.dateNotification <= :dateNotification");
            parameters.put("dateNotification", actionCritereDto.getDateNotification());
            requete.append(" and act.notifier = :notifier");
            parameters.put("notifier", false);
            requete.append(" and attributionRessource.id is not null");
        }
        // Critre sur la date fin de l'action.
        if (actionCritereDto.getDateFinAction() != null) {
            requete.append(" and act.date <= :dateFinAction");
            final Calendar dateFin = actionCritereDto.getDateFinAction();
            dateFin.set(Calendar.HOUR_OF_DAY, HOUR_MAX);
            dateFin.set(Calendar.MINUTE, MINUTE_MAX);
            dateFin.set(Calendar.SECOND, SECONDE_MAX);
            parameters.put("dateFinAction", actionCritereDto.getDateFinAction());
        }
        // Critre sur la reclamation de l'action.
        if (actionCritereDto.getReclamation() != null) {
            requete.append(" and act.reclamation = :reclamation");
            parameters.put("reclamation", actionCritereDto.getReclamation());
        }
        if (actionCritereDto.getIdOpportunite() != null) {
            requete.append(" and affectation.opportunite.id = :idOpportunite");
            parameters.put("idOpportunite", actionCritereDto.getIdOpportunite());
        }
        if (actionCritereDto.getIdPersonne() != null) {
            requete.append(" and affectationPersonne.id = :idPersonne");
            parameters.put("idPersonne", actionCritereDto.getIdPersonne());
        }
        if (actionCritereDto.getIdDocuments() != null && !actionCritereDto.getIdDocuments().isEmpty()) {
            requete.append(" and document.identifiantExterieur in (:idDocuments)");
            parameters.put("idDocuments", actionCritereDto.getIdDocuments());
        }
    }

    // on ne remonte que les actions non supprimees
    requete.append(" and act.supprime =:suppr");
    parameters.put("suppr", false);

    Query query;
    if (addOrderBy) {
        query = createQuery(requete.toString(), criteres);
    } else {
        query = createQuery(requete.toString());
    }
    query.setProperties(parameters);
    return query;
}

From source file:com.square.core.dao.implementations.ActionDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  ww . j  a v  a2 s  .c o m
public List<Action> rechercherActionParCriteresEtRessource(CritereRechercheAction criteres,
        Ressource ressource) {
    final Map<String, Object> params = new HashMap<String, Object>();
    final StringBuffer hqlQuery = new StringBuffer("select act");
    hqlQuery.append(" from Action act");
    hqlQuery.append(" where 1 = 1");
    if (criteres.getDateMinDateDebut() != null && criteres.getDateMaxDateDebut() != null) {
        final String dateMin = "dateMinDateDebut";
        final String dateMax = "dateMaxDateDebut";
        hqlQuery.append(" and act.date between :" + dateMin + " and :" + dateMax);
        params.put(dateMin, criteres.getDateMinDateDebut());
        params.put(dateMax, criteres.getDateMaxDateDebut());
    }
    if (ressource != null) {
        final String param = "idRessource";
        hqlQuery.append(" and act.actionAttribution.ressource.id = :" + param);
        params.put(param, ressource.getId());
    }
    if (criteres.getIdsStatut() != null && !criteres.getIdsStatut().isEmpty()) {
        final String param = "idsStatuts";
        hqlQuery.append(" and act.statut.id in (:" + param + ")");
        params.put(param, criteres.getIdsStatut());
    }
    if (criteres.isVisibleAgenda()) {
        final String param = "visibleAgenda";
        hqlQuery.append(" and act.visibleAgenda = :" + param);
        params.put(param, criteres.isVisibleAgenda());
    }

    // on ne remonte que les actions non supprimees
    hqlQuery.append(" and act.supprime = :suppr");
    params.put("suppr", false);

    hqlQuery.append(" and hour(act.date) > 0");
    final Query query = createQuery(hqlQuery.toString());
    if (!params.isEmpty()) {
        query.setProperties(params);
    }
    return query.list();
}

From source file:com.square.core.dao.implementations.AdresseDaoImplementation.java

License:Open Source License

/**
 * Former une requte pour la recherche d'adresse.
 * @param select la projection./*from   w w  w .  ja v  a2s .c o  m*/
 * @param criteres les criteres de la recherche.
 * @return la requte.
 */
private Query creerQuery(final String select, final AdresseCriteresRechercheDto criteres) {

    final StringBuffer requete = new StringBuffer(select);
    requete.append(" from Personne personne inner join personne.adresses adresse where 1 = 1");
    final Map<String, Object> parameters = new HashMap<String, Object>();
    if (criteres.getDateDebut() != null) {
        requete.append(" and adresse.dateDebut = :dateDebut");
        parameters.put("dateDebut", criteres.getDateDebut());
    }

    if (criteres.getIdPersonne() != null) {
        requete.append(" and personne.id=:idPersonne");
        parameters.put("idPersonne", criteres.getIdPersonne());
    }

    if (criteres.getIdNature() != null) {
        requete.append(" and adresse.nature.id=:idNature");
        parameters.put("idNature", criteres.getIdNature());
    }

    if (criteres.getActive() != null) {
        requete.append(" and adresse.dateFin ");
        if (criteres.getActive()) {
            requete.append("is null");
        } else {
            requete.append("is not null");
        }
    }

    requete.append(" and adresse.supprime = false");

    // Tri sur la nature de l'adresse puis sur la date (adresse en cours en premier : date de fin  null)
    requete.append(" order by adresse.nature.ordre, adresse.dateFin desc, adresse.dateDebut desc");

    final Query query = createQuery(requete.toString());
    query.setProperties(parameters);
    return query;
}

From source file:com.square.core.dao.implementations.EmailDaoImplementation.java

License:Open Source License

/**
 * Former une requte pour la recherche d'email.
 * @param criteres les critres de recherche.
 * @return la requte./*w  w w.  j a  va 2  s .  com*/
 */
private Query creerQuery(final String select, CritereRechercheEmail criteres) {
    final StringBuffer requete = new StringBuffer(select);
    requete.append(" from Email as email");
    requete.append(" inner join email.personnes as personne");
    requete.append(" where 1 = 1");
    final Map<String, Object> parameters = new HashMap<String, Object>();

    if (criteres.getIdsPersonnes() != null && !criteres.getIdsPersonnes().isEmpty()) {
        final String param = "idsPersonnes";
        requete.append(" and personne.id in (:" + param + ")");
        parameters.put(param, criteres.getIdsPersonnes());
    }
    if (criteres.getIdNature() != null) {
        requete.append(" and email.nature.id=:idNature");
        parameters.put("idNature", criteres.getIdNature());
    }
    if (criteres.getEmail() != null && !"".equals(criteres.getEmail())) {
        requete.append(" and email.adresse=:adresseMail");
        parameters.put("adresseMail", criteres.getEmail());
    }
    if (criteres.getIsSupprime() != null) {
        requete.append(" and email.supprime=:isSupprime");
        parameters.put("isSupprime", criteres.getIsSupprime());
    }
    if (!criteres.getIdsEmailsExclus().isEmpty()) {
        requete.append(" and email.id not in ( :idsEmailsExclus )");
        parameters.put("idsEmailsExclus", criteres.getIdsEmailsExclus());
    }

    final Query query = createQuery(requete.toString());
    query.setProperties(parameters);
    return query;
}