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.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }//from   w  w w .j  av  a 2 s  .com
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data;
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public List findBySQLQuery(final String sql, final int begin, final int count, final Map map) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }//from ww  w.j a va  2s . c om
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            if (begin >= 0) {
                query.setFirstResult(begin);
                query.setMaxResults(count);
            }
            return query.list();
        }
    });
}

From source file:com.allinfinance.commquery.dao.CommQueryDAO.java

License:Open Source License

public String findCountBySQLQuery(final String countSql, final Map map) {
    List data = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(countSql);
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next().toString();
                Object obj = map.get(key);
                String[] keys = query.getNamedParameters();
                for (int i = 0; i < keys.length; i++) {
                    if (key != null && key.equals(keys[i])) {
                        if (obj instanceof String) {
                            query.setString(key, obj.toString());
                        }/*w w  w . j a  va  2  s. c om*/
                        if (obj instanceof Number) {
                            query.setInteger(key, Integer.parseInt(obj.toString()));
                        }
                        if (obj instanceof BigDecimal) {
                            query.setBigDecimal(key, (BigDecimal) obj);
                        }
                        if (obj instanceof List) {
                            query.setParameterList(key, (List) obj);
                        }
                    }
                }
            }
            return query.list();
        }
    });
    return data.get(0).toString();
}

From source file:com.cyclopsgroup.tornado.hibernate.HqlLargeList.java

License:CDDL license

/**
 * Overwrite or implement method getSize()
 *
 * @see com.cyclopsgroup.waterview.LargeList#getSize()
 *//*from w  ww.jav a2  s .c  o m*/
public int getSize() throws Exception {
    String countQuery = "SELECT COUNT(*) " + hql;
    Session s = hibernate.getSession(dataSource);
    Query query = s.createQuery(countQuery);
    HashSet parameterNames = new HashSet();
    CollectionUtils.addAll(parameterNames, query.getNamedParameters());
    for (Iterator i = parameters.values().iterator(); i.hasNext();) {
        Parameter p = (Parameter) i.next();
        if (parameterNames.contains(p.getName())) {
            query.setParameter(p.getName(), p.getValue(), p.getType());
        }
    }
    List result = query.list();
    if (result == null || result.isEmpty()) {
        return -1;
    }
    Integer i = (Integer) result.get(0);
    return i.intValue();
}

From source file:com.cyclopsgroup.tornado.hibernate.HqlLargeList.java

License:CDDL license

/**
 * Overwrite or implement method iterate()
 *
 * @see com.cyclopsgroup.waterview.LargeList#iterate(int, int, com.cyclopsgroup.waterview.LargeList.Sorting[])
 *///ww w . j av a 2 s  .c  o  m
public Iterator iterate(int startPosition, int maxRecords, Sorting[] sortings) throws Exception {
    if (StringUtils.isEmpty(hql)) {
        throw new IllegalStateException("query is still emtpy");
    }
    Session s = hibernate.getSession(dataSource);
    StringBuffer sb = new StringBuffer(hql);

    boolean first = true;
    for (int i = 0; i < sortings.length; i++) {
        Sorting sorting = sortings[i];
        if (first) {
            sb.append(" ORDER BY ");
            first = false;
        } else {
            sb.append(", ");
        }
        sb.append(sorting.getName());
        if (sorting.isDescending()) {
            sb.append(" DESC");
        }
    }

    Query q = s.createQuery(sb.toString());
    HashSet parameterNames = new HashSet();
    CollectionUtils.addAll(parameterNames, q.getNamedParameters());
    for (Iterator i = parameters.values().iterator(); i.hasNext();) {
        Parameter p = (Parameter) i.next();
        if (parameterNames.contains(p.getName())) {
            q.setParameter(p.getName(), p.getValue(), p.getType());
        }
    }
    q.setFirstResult(startPosition);
    if (maxRecords > 0) {
        q.setMaxResults(maxRecords);
    }
    return q.iterate();
}

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

private Query setValueQueryCareerTransitionInboxActivityByParam(Query hbm,
        CareerTransitionInboxSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "empName")) {
            hbm.setParameter("empName", "%" + parameter.getEmpName() + "%");
        } else if (StringUtils.equals(param, "empNik")) {
            hbm.setParameter("empNik", "%" + parameter.getEmpNik() + "%");
        } else if (StringUtils.equals(param, "userId")) {
            hbm.setParameter("userId", parameter.getUserId());
        } else if (StringUtils.equals(param, "appDefinitionName")) {
            hbm.setParameter("appDefinitionName", HRMConstant.EMPLOYEE_CAREER_TRANSITION);
        }//  www.  j  a  v a2 s.  c  om
    }
    return hbm;
}

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

private Query setValueQueryEmpEliminationViewModelByParam(Query hbm, EmpEliminationSearchParameter 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, "lastJabatanName")) {
            hbm.setParameter("lastJabatanName", "%" + parameter.getLastJabatanName() + "%");
        }/*from   www.  j  ava2 s.  c o  m*/
    }

    hbm.setParameterList("listCareerHistoryStatus",
            Arrays.asList(HRMConstant.EMP_TERMINATION, HRMConstant.EMP_STOP_CONTRACT, HRMConstant.EMP_LAID_OFF,
                    HRMConstant.EMP_PENSION, HRMConstant.EMP_DISCHAGED));
    return hbm;
}

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

private Query setValueQueryKompetensiJabatanViewModelByParam(Query hbm,
        KompetensiJabatanSearchParameter searchParameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "code")) {
            hbm.setParameter("code", "%" + searchParameter.getCode() + "%");
        } else if (StringUtils.equals(param, "name")) {
            hbm.setParameter("name", "%" + searchParameter.getName() + "%");
        } else if (StringUtils.equals(param, "golJabCode")) {
            hbm.setParameter("golJabCode", "%" + searchParameter.getGolonganJabatan() + "%");
        }//from   w ww.j  a v  a  2 s .c  o  m
    }
    return hbm;
}

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

private Query setValueQueryByParamForPerformanceIndicatorJabatan(Query hbm,
        PerformanceIndicatorJabatanSearchParameter searchParameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "jabatanCode")) {
            hbm.setParameter("jabatanCode", "%" + searchParameter.getJabatanCode() + "%");

        } else if (StringUtils.equals(param, "jabatanName")) {
            hbm.setParameter("jabatanName", "%" + searchParameter.getJabatanName() + "%");

        } else if (StringUtils.equals(param, "golonganJabatan")) {
            hbm.setParameter("golonganJabatan", "%" + searchParameter.getGolonganJabatan() + "%");
        }/*from w w w  .j a v a 2 s  .com*/
    }

    return hbm;
}

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

private Query setValueQueryUndisbursedActivityByParam(Query hbm,
        LoanNewApplicationBoxSearchParameter parameter) {
    for (String param : hbm.getNamedParameters()) {
        if (StringUtils.equals(param, "empName")) {
            hbm.setParameter("empName", "%" + parameter.getEmpName() + "%");
        } else if (StringUtils.equals(param, "empNik")) {
            hbm.setParameter("empNik", "%" + parameter.getEmpNik() + "%");
        } else if (StringUtils.equals(param, "loanTypeName")) {
            hbm.setParameter("loanTypeName", "%" + parameter.getLoanTypeName() + "%");
        } else if (StringUtils.equals(param, "userId")) {
            hbm.setParameter("userId", parameter.getUserId());
        } else if (StringUtils.equals(param, "appDefinitionName")) {
            hbm.setParameter("appDefinitionName", HRMConstant.LOAN);
        } else if (StringUtils.equals(param, "companyId")) {
            hbm.setParameter("companyId", HrmUserInfoUtil.getCompanyId());
        } else if (StringUtils.equals(param, "approvalStatus")) {
            hbm.setParameter("approvalStatus", parameter.getApprovalStatus());
        }//from   www.  j  a  v a2s .  co  m
    }
    return hbm;
}