Example usage for org.hibernate Query getNamedParameters

List of usage examples for org.hibernate Query getNamedParameters

Introduction

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

Prototype

@Deprecated
String[] getNamedParameters();

Source Link

Document

Return the names of all named parameters of the query.

Usage

From source file:com.inkubator.hrm.dao.impl.LoanNewCancelationDaoImpl.java

private Query setValueQueryByParam(Query hbm, LoanNewCancelationBoxSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "codeApplication")) {
            hbm.setParameter("codeApplication", "%" + parameter.getCodeApplication() + "%");
        } else if (StringUtils.equals(param, "codeCancellation")) {
            hbm.setParameter("codeCancellation", "%" + parameter.getCodeCancelation() + "%");
        } else if (StringUtils.equals(param, "loanNewTypeName")) {
            hbm.setParameter("loanNewTypeName", "%" + parameter.getLoanNewTypeName() + "%");
        } else if (StringUtils.equals(param, "empName")) {
            hbm.setParameter("empName", "%" + parameter.getEmpName() + "%");
        } else if (StringUtils.equals(param, "userId")) {
            hbm.setParameter("userId", parameter.getUserId());
        }//from  w ww. j  a va  2 s.c  o  m
    }
    return hbm;
}

From source file:com.inkubator.hrm.dao.impl.LogWtAttendanceRealizationDaoImpl.java

private Query setValueQueryLogWtAttendanceCalculationByParam(Query hbm,
        WtAttendanceCalculationSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "empName")) {
            hbm.setParameter("empName", "%" + parameter.getEmpName() + "%");
        } else if (StringUtils.equals(param, "nik")) {
            hbm.setParameter("nik", "%" + parameter.getNik() + "%");
        } else if (StringUtils.equals(param, "wtGroupWorkingName")) {
            hbm.setParameter("wtGroupWorkingName", "%" + parameter.getWtGroupWorkingName() + "%");
        }/*from   www .j  a va 2  s  .c  o m*/
    }
    return hbm;
}

From source file:com.inkubator.hrm.dao.impl.RecruitSelectionApplicantInitialDaoImpl.java

private Query setValueQueryRecruitmentScheduleSettingByParam(Query hbm,
        RecruitmentScheduleSettingSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "careerCandidate")) {
            hbm.setParameter("careerCandidate", parameter.getCandidateStatusId());
        } else if (StringUtils.equals(param, "listJabatanOnSelectedMppApply")) {
            hbm.setParameterList("listJabatanOnSelectedMppApply", parameter.getListJabatanOnSelectedMppApply());
        }//from w w w . jav  a  2s .  c o m
    }
    return hbm;
}

From source file:com.inkubator.hrm.dao.impl.TempAttendanceRealizationDaoImpl.java

private Query setValueQueryWtAttendanceCalculationByParam(Query hbm,
        WtAttendanceCalculationSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "empName")) {
            hbm.setParameter("empName", "%" + parameter.getEmpName() + "%");
        } else if (StringUtils.equals(param, "nik")) {
            hbm.setParameter("nik", "%" + parameter.getNik() + "%");
        } else if (StringUtils.equals(param, "wtGroupWorkingName")) {
            hbm.setParameter("wtGroupWorkingName", "%" + parameter.getWtGroupWorkingName() + "%");
        }//from   ww w  .ja v a2 s .co m
    }
    return hbm;
}

From source file:com.inkubator.hrm.dao.impl.WtPeriodeDaoImpl.java

private Query setValueQueryWtPeriodEmpByParam(Query hbm, WtPeriodeEmpSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "startPeriod")) {
            hbm.setParameter("startPeriod", parameter.getStartPeriod());
        } else if (StringUtils.equals(param, "endPeriod")) {
            hbm.setParameter("endPeriod", parameter.getEndPeriod());
        }/*w w w.jav a2  s  .c om*/
    }
    return hbm;
}

From source file:com.mg.jet.birt.report.data.oda.ejbql.Statement.java

License:Open Source License

public void prepare(String query) throws OdaException {
    Query qry = null;
    // Test the connection
    testConnection();/*from  w w w.ja  v a2 s  . co  m*/
    // holds the column types
    List<String> arColsType = new ArrayList<String>();
    List<String> arCols = new ArrayList<String>();
    List<String> arColClass = new ArrayList<String>();

    // holds the column names, also used for labels
    String[] props = null;
    try {

        Session hibsession = HibernateUtil.currentSession();
        // Create a Hibernate Query
        query = query.replaceAll("[\\n\\r]+", " ");
        query = query.trim();
        qry = hibsession.createQuery(query);

        // Get the list of return types from the query
        Type[] qryReturnTypes = qry.getReturnTypes();
        paramNames = Arrays.asList(qry.getNamedParameters());
        paramValues = new Object[paramNames.size()];

        // When specifing the HQL "from object" the returned type is a
        // Hibernate EntityType
        // When the columns are specified the returned values are normal
        // data types
        // The first half of this if statment handles the EntityType, the
        // else handles the
        // other case.
        // We are not handling multipe result sets.
        if (qryReturnTypes.length > 0 && qryReturnTypes[0].isEntityType()) {
            for (int j = 0; j < qryReturnTypes.length; j++) {
                // Get the classname and use utility function to retrieve
                // data types
                String clsName = qryReturnTypes[j].getName();
                // props holds the column names
                props = HibernateUtil.getHibernateProp(clsName);
                for (int x = 0; x < props.length; x++) {
                    String propType = HibernateUtil.getHibernatePropTypes(clsName, props[x]);
                    // Verify that the data type is valid
                    if (DataTypes.isValidType(propType)) {
                        arColsType.add(propType);
                        // Only do this on Entity Types so we dont have a
                        // name collision
                        arCols.add(props[x]);
                        arColClass.add(clsName);
                    } else {
                        throw new OdaException(
                                Messages.getString("Statement.SOURCE_DATA_ERROR") + " " + propType);
                    }
                }
            }
        } else {
            // Extract the column names from the query
            props = extractColumns(qry.getQueryString());
            // Get the return types from the Type[]
            for (int t = 0; t < qryReturnTypes.length; t++) {
                // Verify that the data type is valid
                String propType = qryReturnTypes[t].getName();
                if (DataTypes.isValidType(propType)) {
                    arColsType.add(qryReturnTypes[t].getName());
                    arCols.add(props[t]);
                } else {
                    throw new OdaException(Messages.getString("Statement.SOURCE_DATA_ERROR") + " " + propType);
                }
            }

        }
    } catch (OdaException e) {
        throw e;
    } catch (Exception e) {
        throw new OdaException(e);
    }
    // this example does not enforce unique column names
    // Create a new ResultSetMetaData object passing in the column names and
    // data types

    // Have to remove . which BIRT does not allow
    String[] arLabels = (String[]) arCols.toArray(new String[arCols.size()]);
    for (int j = 0; j < arLabels.length; j++) {
        arLabels[j] = arLabels[j].replace('.', ':');
    }

    this.resultSetMetaData = new ResultSetMetaData(arLabels,
            (String[]) arColsType.toArray(new String[arColsType.size()]), arLabels,
            (String[]) arColClass.toArray(new String[arColClass.size()]));
    // query is saved for execution
    this.query = query;

}

From source file:de.innovationgate.webgate.api.jdbc.HibernateResultSet.java

License:Open Source License

public static void injectQueryParams(Query hibQuery, Map map) throws WGAPIException {

    if (map == null || map.size() == 0) {
        return;//from  www  . j a v a2 s.c o m
    }

    String[] pNames = hibQuery.getNamedParameters();
    for (int i = 0; i < pNames.length; i++) {
        String pName = pNames[i];
        Object value = map.get(pName);
        if (value != null) {
            if (value instanceof WGDocument) {
                value = ((WGDocument) value).getNativeObject();
            }
            if (value instanceof Double) {
                hibQuery.setDouble(pName, (Double) value);
            } else if (value instanceof Collection<?>) {
                hibQuery.setParameterList(pName, (Collection<?>) value);
            } else {
                hibQuery.setParameter(pName, value);
            }
        }
    }

}

From source file:gov.nih.nci.cadsrapi.dao.orm.CartORMDAOImpl.java

License:BSD License

public List<Cart> cartSearch2(Cart exampleCart) throws DAOException, Exception {
    List<Cart> results = new ArrayList<Cart>();
    Session session = getSession();/*w  ww  .  j a  v a2s  . c o  m*/

    //Transaction t = session.beginTransaction();
    StringBuilder query = new StringBuilder();
    query.append("from gov.nih.nci.cadsr.objectcart.domain.Cart where");

    if (exampleCart.getId() != null)
        query.append(" id = :cartId");
    else {
        int andCntr = 0;
        if (exampleCart.getUserId() != null && exampleCart.getUserId().length() > 0) {
            query.append(" userId = :userId");
            andCntr++;
        }
        if (exampleCart.getName() != null && exampleCart.getName().length() > 0) {
            if (andCntr > 0)
                query.append(" and");
            query.append(" name = :name");
            andCntr++;
        }
        /*
        if (exampleCart.getType() != null && exampleCart.getType().length() > 0){
           if (andCntr >0)
              query.append(" and");
           query.append(" type = :type");
        }*/
    }
    query.append(" and (expirationDate > :expirationDate or expirationDate is null)");

    Query q = session.createQuery(query.toString());
    String[] params = q.getNamedParameters();

    for (String param : params) {
        if ("cartId".equals(param))
            q.setInteger(param, exampleCart.getId());
        else {
            if ("userId".equals(param))
                q.setString(param, exampleCart.getUserId());
            if ("name".equals(param))
                q.setString(param, exampleCart.getName());
            /*if ("type".equals(param))
               q.setString(param, exampleCart.getType());*/
        }
    }

    q.setTimestamp("expirationDate", new Timestamp(System.currentTimeMillis()));

    try {
        results = (List<Cart>) q.list();

    } catch (JDBCException ex) {
        ex.printStackTrace();
        log.error("JDBC Exception in ORMDAOImpl ", ex);
        throw new DAOException("JDBC Exception in ORMDAOImpl ", ex);
    } catch (org.hibernate.HibernateException hbmEx) {
        hbmEx.printStackTrace();
        log.error(hbmEx.getMessage());
        throw new DAOException("DAO:Hibernate problem ", hbmEx);
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Exception ", e);
        throw new DAOException("Exception in ORMDAOImpl ", e);
    } finally {
        try {
            //t.commit();
            session.close();
        } catch (Exception eSession) {
            log.error("Could not close the session - " + eSession.getMessage());
            throw new DAOException("Could not close the session  " + eSession);
        }
    }
    return results;
}

From source file:gov.nih.nci.objectCart.dao.orm.CartORMDAOImpl.java

License:BSD License

public List<Cart> cartSearch(Cart exampleCart) throws DAOException, Exception {
    List<Cart> results = new ArrayList<Cart>();
    Session session = getSession();/*from  w  w  w.j a v  a  2 s. com*/

    Transaction t = session.beginTransaction();
    StringBuilder query = new StringBuilder();
    query.append("from Cart where");

    if (exampleCart.getId() != null)
        query.append(" id = :cartId");
    else {
        int andCntr = 0;
        if (exampleCart.getUserId() != null && exampleCart.getUserId().length() > 0) {
            query.append(" userId = :userId");
            andCntr++;
        }
        if (exampleCart.getName() != null && exampleCart.getName().length() > 0) {
            if (andCntr > 0)
                query.append(" and");
            query.append(" name = :name");
            andCntr++;
        }
        /*
        if (exampleCart.getType() != null && exampleCart.getType().length() > 0){
           if (andCntr >0)
              query.append(" and");
           query.append(" type = :type");
        }*/
    }
    query.append(" and (expirationDate > :expirationDate or expirationDate is null)");

    Query q = session.createQuery(query.toString());
    String[] params = q.getNamedParameters();

    for (String param : params) {

        if ("cartId".equals(param))
            q.setInteger(param, exampleCart.getId());
        else {
            if ("userId".equals(param))
                q.setString(param, exampleCart.getUserId());
            if ("name".equals(param))
                q.setString(param, exampleCart.getName());
            /*if ("type".equals(param))
               q.setString(param, exampleCart.getType());*/
        }
    }

    q.setTimestamp("expirationDate", new Timestamp(System.currentTimeMillis()));

    try {
        results = (List<Cart>) q.list();

    } catch (JDBCException ex) {
        log.error("JDBC Exception in ORMDAOImpl ", ex);
        throw new DAOException("JDBC Exception in ORMDAOImpl ", ex);
    } catch (org.hibernate.HibernateException hbmEx) {
        log.error(hbmEx.getMessage());
        throw new DAOException("DAO:Hibernate problem ", hbmEx);
    } catch (Exception e) {
        log.error("Exception ", e);
        throw new DAOException("Exception in ORMDAOImpl ", e);
    } finally {
        try {
            t.commit();
            session.close();
        } catch (Exception eSession) {
            log.error("Could not close the session - " + eSession.getMessage());
            throw new DAOException("Could not close the session  " + eSession);
        }
    }
    return results;
}

From source file:io.datalayer.sql.dao.impl.GenericDaoHibernateImpl.java

License:Apache License

private Query prepareQuery(Method method, Object[] queryArgs) {
    final String queryName = getNamingStrategy().queryNameFromMethod(type, method);
    final Query namedQuery = getSession().getNamedQuery(queryName);
    String[] namedParameters = namedQuery.getNamedParameters();
    if (namedParameters.length == 0) {
        setPositionalParams(queryArgs, namedQuery);
    } else {/*from w  w w  .ja  va2 s.co m*/
        setNamedParams(namedParameters, queryArgs, namedQuery);
    }
    return namedQuery;
}