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:chem.anim.AnimatedDrawing.java

@Override
public void saveToDatabase(Session session, int documentId) {
    m_model.save(session, documentId);//  w  ww  .j  av a  2  s  . c  o  m

    for (Entry<Integer, String> entry : m_deleteCache.entrySet()) {
        if (entry.getValue().equalsIgnoreCase("AtomModel")) {
            String elHql = "DELETE FROM ElectronModel WHERE atomId = " + entry.getKey();
            Query query = session.createQuery(elHql);
            int rows = query.executeUpdate();
            System.out.println("Rows affected by ElectronModel delete: " + rows);
        }

        String hql = "DELETE FROM " + entry.getValue() + " WHERE id = :delId";
        Query query = session.createQuery(hql);
        query.setParameter("delId", entry.getKey());
        int rows = query.executeUpdate();
        System.out.println("Rows affected by " + entry.getValue() + ": " + rows);
    }

    FigureEnumeration figures = figures();

    while (figures.hasMoreElements()) {
        Figure fig = figures.nextFigure();
        PersistableFigure pf = (PersistableFigure) fig;
        pf.saveToDatabase(session, m_model.getId());
    }
}

From source file:clases.EmpleadosHelper.java

public void incrementarSalarioEmpleado(int empleado, int incremento) {
    this.tx = this.session.beginTransaction();
    Query query = session.createSQLQuery("CALL AUMENTARSALARIOEMP(?,?)");
    query.setParameter(0, empleado);
    query.setParameter(1, incremento);//from   w ww  .j  ava2s . c o m
    query.executeUpdate();
    tx.commit();
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 * /*from ww w . j av a2  s  .  c o m*/
 * 
 * @param hql
 * @param current_page
 * @param max_num
 * @param map
 * @return
 */
public List<T> findForPage(String hql, int current_page, int max_num, Map<String, Object> map) {
    List<T> result = null;
    if (current_page <= 0) {
        current_page = 1;
    }
    if (max_num > 20) {
        max_num = 20;
    } else if (max_num <= 0) {
        max_num = 1;
    }
    try {
        if (!hql.contains("where")) {
            hql = hql.concat(" where 1=1 ");
        }
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                hql = hql.concat(" and " + key + " =:" + key);
            }
        }
        Query query = this.getSession().createQuery(hql);
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            query.setParameter(key, map.get(key));
        }
        query.setFirstResult(((current_page - 1)) * max_num);
        query.setMaxResults(max_num);
        result = query.list();
    } catch (RuntimeException re) {
        throw re;
    }
    return result;
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 * ????/*from  w  w w .  j  a v a 2s  .  com*/
 * 
 * @param hql
 * @param map
 * @return
 */
public int count(String hql, Map<String, Object> map) {
    int result = 0;
    try {
        if (!hql.contains("where")) {
            hql = hql.concat(" where 1=1 ");
        }
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                hql = hql.concat(" and " + key + " =:" + key);
            }
        }
        Query query = this.getSession().createQuery(hql);
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            query.setParameter(key, map.get(key));
        }
        result = query.list().size();
    } catch (RuntimeException re) {
        throw re;
    }
    return result;
}

From source file:cn.edu.xjtu.manage.dao.LicenseDao.java

License:Open Source License

public int updateLicense(int id, String license, Date expires, int isvalid) {
    HibernateSessionManager.getThreadLocalTransaction();
    if (id < 1) {
        Query q = session.createSQLQuery("insert into t_license(license,expires,isvalid) values (?,?,?)");
        q.setParameter(0, license);
        q.setParameter(1, expires);/*from  w w w.j  ava 2  s  .c  o m*/
        q.setParameter(2, isvalid);
        int result = q.executeUpdate();
        return result;
    } else {
        SQLQuery q = session
                .createSQLQuery("update t_license t set t.license=?,t.expires=?,t.isvalid=? where t.ID=?");
        q.setParameter(0, license);
        q.setParameter(1, expires);
        q.setParameter(2, isvalid);
        q.setParameter(3, id);
        int re = q.executeUpdate();
        return re;
    }
}

From source file:cn.hxh.springside.orm.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * ?HQL?Query.//from w  w  w . j a v a 2  s. c om
 * find()???.
 * 
 * @param values ????,?.
 */
public Query createQuery(final String queryString, final Object... values) {
    AssertUtils.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:cn.lhfei.identity.orm.persistence.impl.IdentityDAOImpl.java

License:Apache License

@Override
public int restPassword(String userId, String password) {
    int result = -1;

    String hql = "update User set passWord = :password where userId = :userId ";

    Session session = null;/*from  w  w  w  .  j a v a 2s  . com*/

    try {
        session = sessionFactory.openSession();

        Query query = session.createQuery(hql);

        query.setParameter("password", password);
        query.setParameter("userId", userId);

        result = query.executeUpdate();

        return result;

    } catch (HibernateException e) {

        return result;

    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
}

From source file:cn.trymore.core.dao.impl.DAOGenericImpl.java

License:Open Source License

public Object findUnique(final String hsql, final Object[] params) {
    return getHibernateTemplate().execute(new HibernateCallback() {

        @Override//from   w  w w  .  j a  v a2 s  . c o m
        public Object doInHibernate(Session paramSession) throws HibernateException, SQLException {
            Query localQuery = paramSession.createQuery(hsql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    localQuery.setParameter(i, params[i]);
                }
            }

            return localQuery.uniqueResult();
        }
    });
}