Example usage for org.hibernate SQLQuery setParameterList

List of usage examples for org.hibernate SQLQuery setParameterList

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setParameterList.

Prototype

@Override
    NativeQuery<T> setParameterList(String name, Object[] values);

Source Link

Usage

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public Map<Integer, String> getPreferredIdsByGIDs(final List<Integer> gids) {
    final Map<Integer, String> toreturn = new HashMap<>();
    for (final Integer gid : gids) {
        toreturn.put(gid, null);/*w ww.  j a  va  2 s.  com*/
    }

    try {
        final SQLQuery query = this.getSession().createSQLQuery(Name.GET_PREFERRED_IDS_BY_GIDS);
        query.setParameterList("gids", gids);

        final List<Object> results = query.list();
        for (final Object result : results) {
            final Object[] resultArray = (Object[]) result;
            final Integer gid = (Integer) resultArray[0];
            final String preferredId = (String) resultArray[1];
            toreturn.put(gid, preferredId);
        }
    } catch (final HibernateException e) {
        final String message = "Error with getPreferredIdsByGIDs(gids=" + gids + ") query from Name "
                + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }

    return toreturn;
}

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public Map<Integer, String> getPreferredNamesByGIDs(final List<Integer> gids) {
    final Map<Integer, String> toreturn = new HashMap<>();
    for (final Integer gid : gids) {
        toreturn.put(gid, null);//w  ww .  j  a  va2  s  .co  m
    }

    try {
        final SQLQuery query = this.getSession().createSQLQuery(Name.GET_PREFERRED_NAMES_BY_GIDS);
        query.setParameterList("gids", gids);

        final List<Object> results = query.list();
        for (final Object result : results) {
            final Object[] resultArray = (Object[]) result;
            final Integer gid = (Integer) resultArray[0];
            final String preferredId = (String) resultArray[1];
            toreturn.put(gid, preferredId);
        }
    } catch (final HibernateException e) {
        final String message = "Error with getPreferredNamesByGIDs(gids=" + gids + ") query from Name "
                + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }

    return toreturn;
}

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

public Map<Integer, Integer> getPreferredNameIdsByGIDs(final List<Integer> gids) {
    final Map<Integer, Integer> toreturn = new HashMap<>();
    for (final Integer gid : gids) {
        toreturn.put(gid, null);//from w w w.  j a  v a  2s. c  o  m
    }

    try {
        final SQLQuery query = this.getSession().createSQLQuery(Name.GET_PREFERRED_NAME_IDS_BY_GIDS);
        query.setParameterList("gids", gids);

        final List<Object> results = query.list();
        for (final Object result : results) {
            final Object[] resultArray = (Object[]) result;
            final Integer gid = (Integer) resultArray[0];
            final Integer preferredId = (Integer) resultArray[1];
            toreturn.put(gid, preferredId);
        }
    } catch (final HibernateException e) {
        throw new MiddlewareQueryException(
                "Error with getPreferredNameIdsByGIDs(gids=" + gids + ") query from Name " + e.getMessage(), e);
    }

    return toreturn;
}

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

public Map<Integer, String> getSourcePreferredNamesByGids(final List<Integer> gids) {
    final Map<Integer, String> map;

    if (gids == null || gids.isEmpty()) {
        return new HashMap<>();
    }/*from   ww  w  .j  av  a 2s  .c o  m*/

    try {
        final SQLQuery query = this.getSession()
                .createSQLQuery(Name.GET_GROUP_SOURCE_PREFERRED_NAME_IDS_BY_GIDS);
        query.setParameterList("gids", gids);

        map = this.createGidAndPreferredNameMap(query.list());

    } catch (final HibernateException e) {
        final String message = "Error with getSourcePreferredNamesByGids(gids=" + gids + ") query from Name "
                + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }

    return map;

}

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

public Map<Integer, String> getImmediatePreferredNamesByGids(final List<Integer> gids) {
    final Map<Integer, String> map;

    if (gids == null || gids.isEmpty()) {
        return new HashMap<>();
    }//from   ww  w  . jav a 2  s  . c o m

    try {
        final SQLQuery query = this.getSession()
                .createSQLQuery(Name.GET_IMMEDIATE_SOURCE_PREFERRED_NAME_IDS_BY_GIDS);
        query.setParameterList("gids", gids);

        map = this.createGidAndPreferredNameMap(query.list());

    } catch (final HibernateException e) {
        final String message = "Error with getImmediatePreferredNamesByGids(gids=" + gids + ") query from Name "
                + e.getMessage();
        NameDAO.LOG.error(message);
        throw new MiddlewareQueryException(message, e);
    }
    return map;
}

From source file:org.generationcp.middleware.dao.NameDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Name> getNamesByTypeAndGIDList(final Integer nameType, final List<Integer> gidList) {
    List<Name> returnList = new ArrayList<>();
    if (gidList != null && !gidList.isEmpty()) {
        try {//from www. j  a v  a2 s. com
            final String sql = "SELECT {n.*}" + " FROM names n" + " WHERE n.ntype = :nameType"
                    + " AND n.gid in (:gidList)";
            final SQLQuery query = this.getSession().createSQLQuery(sql);
            query.addEntity("n", Name.class);
            query.setParameter("nameType", nameType);
            query.setParameterList("gidList", gidList);
            returnList = query.list();
        } catch (final HibernateException e) {
            throw new MiddlewareQueryException("Error with getNamesByTypeAndGIDList(nameType=" + nameType
                    + ", gidList=" + gidList + "): " + e.getMessage(), e);
        }
    }
    return returnList;
}

From source file:org.generationcp.middleware.dao.NumericDataDAO.java

License:Open Source License

@SuppressWarnings("rawtypes")
public List<NumericDataElement> getValuesByOunitIDList(List<Integer> ounitIdList)
        throws MiddlewareQueryException {

    if (ounitIdList == null || ounitIdList.isEmpty()) {
        return new ArrayList<NumericDataElement>();
    }//from ww  w. j av  a  2s.com

    try {
        SQLQuery query = getSession().createSQLQuery(NumericData.GET_BY_OUNIT_ID_LIST);
        query.setParameterList("ounitIdList", ounitIdList);

        List<NumericDataElement> dataValues = new ArrayList<NumericDataElement>();

        List results = query.list();
        for (Object o : results) {
            Object[] result = (Object[]) o;
            if (result != null) {
                Integer ounitId = (Integer) result[0];
                Integer variateId = (Integer) result[1];
                String variateName = (String) result[2];
                Double value = (Double) result[3];

                NumericDataElement dataElement = new NumericDataElement(ounitId, variateId, variateName, value);

                dataValues.add(dataElement);
            }
        }

        return dataValues;
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with getValuesByOunitIDList(ounitIdList=" + ounitIdList
                + ") query from NumericData: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.NumericLevelDAO.java

License:Open Source License

@SuppressWarnings("rawtypes")
public List<NumericLevelElement> getValuesByOunitIDList(List<Integer> ounitIdList)
        throws MiddlewareQueryException {

    List<NumericLevelElement> levelValues = new ArrayList<NumericLevelElement>();

    if (ounitIdList == null || ounitIdList.isEmpty()) {
        return levelValues;
    }/*from   w  w  w. j  av  a 2s. co  m*/

    try {
        SQLQuery query = getSession().createSQLQuery(NumericLevel.GET_BY_OUNIT_ID_LIST);
        query.setParameterList("ounitIdList", ounitIdList);

        List results = query.list();
        for (Object o : results) {
            Object[] result = (Object[]) o;
            if (result != null) {
                Integer ounitId = (Integer) result[0];
                Integer factorId = (Integer) result[1];
                String factorName = (String) result[2];
                Double value = (Double) result[3];

                NumericLevelElement levelElement = new NumericLevelElement(ounitId, factorId, factorName,
                        value);

                levelValues.add(levelElement);
            }
        }

        return levelValues;
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with getValuesByOunitIDList(ounitIdList=" + ounitIdList
                + ") query from NumericLevel " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.oms.CVTermDao.java

License:Open Source License

public Map<String, Map<Integer, VariableType>> getTermIdsWithTypeByNameOrSynonyms(
        final List<String> nameOrSynonyms, final int cvId) {
    final Map<String, Map<Integer, VariableType>> stdVarMap = new HashMap<String, Map<Integer, VariableType>>();

    // Store the names in the map in uppercase
    for (int i = 0, size = nameOrSynonyms.size(); i < size; i++) {
        nameOrSynonyms.set(i, nameOrSynonyms.get(i).toUpperCase());
    }//from  w  ww  . j  a  v a  2  s.c om

    try {
        if (!nameOrSynonyms.isEmpty()) {

            final StringBuilder sqlString = new StringBuilder().append("SELECT cvt.name, cvt.cvterm_id ")
                    .append("FROM cvterm cvt ")
                    .append("WHERE cvt.cv_id = :cvId and cvt.name IN (:nameOrSynonyms) AND cvt.is_obsolete = 0 ")
                    .append("UNION ").append("SELECT syn.synonym, cvt.cvterm_id ")
                    .append("FROM cvterm cvt INNER JOIN cvtermsynonym syn ON  syn.cvterm_id = cvt.cvterm_id ")
                    .append("AND cvt.cv_id = :cvId AND syn.synonym IN (:nameOrSynonyms) AND cvt.is_obsolete = 0");

            final SQLQuery query = this.getSession().createSQLQuery(sqlString.toString());
            query.setParameter("cvId", cvId);
            query.setParameterList("nameOrSynonyms", nameOrSynonyms);

            final List<Object[]> results = query.list();

            for (final Object[] row : results) {
                final String nameOrSynonym = ((String) row[0]).trim().toUpperCase();
                final Integer cvtermId = (Integer) row[1];

                Map<Integer, VariableType> stdVarIdsWithType = null;
                if (stdVarMap.containsKey(nameOrSynonym)) {
                    stdVarIdsWithType = stdVarMap.get(nameOrSynonym);
                } else {
                    stdVarIdsWithType = new HashMap<Integer, VariableType>();
                    stdVarMap.put(nameOrSynonym, stdVarIdsWithType);
                }
                stdVarIdsWithType.put(cvtermId, this.getDefaultVariableType(cvtermId));
            }

        }

    } catch (final HibernateException e) {
        this.logAndThrowException(
                "Error in getTermsByNameOrSynonyms=" + nameOrSynonyms + " in CVTermDao: " + e.getMessage(), e);
    }
    return stdVarMap;
}

From source file:org.generationcp.middleware.dao.oms.CVTermDao.java

License:Open Source License

public List<CVTerm> getValidCvTermsByIds(final List<Integer> ids, final int storedInId, final int dataTypeId) {
    final List<CVTerm> terms = new ArrayList<>();

    if (ids != null && !ids.isEmpty()) {
        try {/*from  ww w .ja v a 2  s .c  om*/
            final StringBuilder queryString = new StringBuilder()
                    .append("SELECT cvt.cvterm_id, cvt.name, cvt.definition ").append("FROM cvterm cvt ")
                    .append("INNER JOIN cvterm_relationship datatype ON datatype.subject_id = cvt.cvterm_id ")
                    .append(" AND datatype.type_id = ").append(TermId.HAS_TYPE.getId())
                    .append(" INNER JOIN cvterm_relationship stored_in ON datatype.subject_id = stored_in.subject_id ")
                    .append(" AND stored_in.type_id = ").append(TermId.STORED_IN.getId())
                    .append(" WHERE cvt.cvterm_id in (:ids)")
                    .append(" AND (stored_in.object_id <> :storedIn OR (stored_in.object_id = :storedIn ")
                    .append(" AND datatype.object_id = :datatype))");

            final SQLQuery query = this.getSession().createSQLQuery(queryString.toString());
            query.setParameterList("ids", ids);
            query.setParameter("storedIn", storedInId);
            query.setParameter("datatype", dataTypeId);

            final List<Object[]> list = query.list();

            for (final Object[] row : list) {
                final Integer id = (Integer) row[0];
                final String name = (String) row[1];
                final String definition = (String) row[2];

                final CVTerm cvTerm = new CVTerm();
                cvTerm.setCvTermId(id);
                cvTerm.setName(name);
                cvTerm.setDefinition(definition);
                terms.add(cvTerm);
            }
        } catch (final HibernateException e) {
            this.logAndThrowException(
                    "Error at getValidCvTermsByIds=" + ids + " query on CVTermDao: " + e.getMessage(), e);
        }
    }
    return terms;
}