Example usage for org.hibernate Session getNamedQuery

List of usage examples for org.hibernate Session getNamedQuery

Introduction

In this page you can find the example usage for org.hibernate Session getNamedQuery.

Prototype

org.hibernate.Query getNamedQuery(String queryName);

Source Link

Document

Create a Query instance for the named query.

Usage

From source file:com.liusoft.dlog4j.dao.UserDAO.java

License:Open Source License

/**
 * keep_days,/*from w  ww  .j  ava 2  s  . c  o m*/
 * @param userid
 * @param lastLogin 
 * @param manual_logout 
 * @return
 */
public static int userLogout(int userid, Timestamp lastLogin, boolean manual_logout) {
    Session ssn = getSession();
    if (ssn == null)
        return -1;
    try {
        beginTransaction();
        Query q = ssn.getNamedQuery(manual_logout ? "USER_LOGOUT_1" : "USER_LOGOUT_2");
        q.setInteger("online_status", UserBean.STATUS_OFFLINE);
        if (manual_logout)
            q.setInteger("keep_day", 0);
        q.setInteger("user_id", userid);
        q.setTimestamp("last_time", lastLogin);
        int er = q.executeUpdate();
        commit();
        return er;
    } catch (HibernateException e) {
        rollback();
        throw e;
    }
}

From source file:com.liusoft.dlog4j.dao.VisitStatDAO.java

License:Open Source License

/**
 * /*w ww.  j  a  va2 s . c o  m*/
 * TODO: ,
 * @param ssb
 * @throws Exception 
 */
public static void writeStatData(int siteid, int uvCount, int source) throws Exception {
    Calendar cal = Calendar.getInstance();
    int statDate = getStatDate(cal);
    Session ssn = getSession();
    try {
        beginTransaction();
        Query update_q = ssn.getNamedQuery((siteid > 0) ? "UPDATE_SITE_STAT_1" : "UPDATE_SITE_STAT_2");
        update_q.setInteger(0, uvCount);
        update_q.setInteger(1, statDate);
        update_q.setInteger(2, source);
        if (siteid > 0)
            update_q.setInteger(3, siteid);
        if (update_q.executeUpdate() < 1) {
            SiteStatBean ssb = new SiteStatBean();
            ssb.setSiteId(siteid);
            ssb.setUvCount(uvCount);
            ssb.setUpdateTime(new Date());
            ssb.setSource(source);
            ssb.setStatDate(statDate);
            ssn.save(ssb);
        }
        commit();
    } catch (Exception e) {
        rollback();
        throw e;
    }
}

From source file:com.luis.servicios.ManagerConocimientos.java

public Collection<Conocimientos> consultarDatos(String consulta, Map<String, Object> params) {

    Session ses = getHibernateTemplate().getSessionFactory().getCurrentSession();
    Query q = ses.getNamedQuery(consulta);

    for (String object : params.keySet()) {
        q.setParameter(object, params.get(object));
    }//from www.java  2  s  . co m

    Collection<Conocimientos> l = q.list();

    return l;

}

From source file:com.luis.servicios.ManagerEmpleados.java

public Collection<Empleado> consultarDatos(String consulta, Map<String, Object> params) {

    Session ses = getHibernateTemplate().getSessionFactory().getCurrentSession();
    Query q = ses.getNamedQuery(consulta);

    for (String object : params.keySet()) {
        q.setParameter(object, params.get(object));
    }// w ww  .ja  v a  2  s.c  o m

    Collection<Empleado> l = q.list();

    return l;

}

From source file:com.luis.servicios.ManagerPuestos.java

public Collection<Puesto> consultarDatos(String consulta, Map<String, Object> params) {

    Session ses = getHibernateTemplate().getSessionFactory().getCurrentSession();
    Query q = ses.getNamedQuery(consulta);

    for (String object : params.keySet()) {
        q.setParameter(object, params.get(object));
    }/*from www .j  a v  a2 s .  c o  m*/

    Collection<Puesto> l = q.list();

    return l;

}

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

License:Open Source License

/**
 * ?   EJBQL ?  /*  w  w w. j a va  2 s .  c o  m*/
 *
 * @param queryName ? ?
 * @param values    ? 
 * @return  ?
 */
public List findByNamedQuery(final String queryName, final Object[] values) {
    return (List) execute(new HibernateCallback<List>() {
        public List doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            prepareQuery(queryObject);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    queryObject.setParameter(i, values[i]);
                }
            }
            return queryObject.list();
        }
    });
}

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

License:Open Source License

/**
 * ?   EJBQL ?   /*from  ww  w  .j  av a 2 s. com*/
 *
 * @param queryName  ? ?
 * @param paramNames  
 * @param values     ? 
 * @return  ?
 */
public <T> List<T> findByNamedQueryAndNamedParam(final String queryName, final String[] paramNames,
        final Object[] values, final ResultTransformer<T> resultTransformer) {

    if (paramNames != null && values != null && paramNames.length != values.length) {
        throw new IllegalArgumentException("Length of paramNames array must match length of values array");
    }
    return (List<T>) execute(new HibernateCallback<List<T>>() {
        @SuppressWarnings("unchecked")
        public List<T> doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            setResultTransformer(queryObject, resultTransformer);
            prepareQuery(queryObject);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
                }
            }
            return queryObject.list();
        }
    });
}

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

License:Open Source License

/**
 * ?   EJBQL ?   //from   w  w w.  jav  a  2 s. co  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.mg.framework.api.orm.OrmTemplate.java

License:Open Source License

/**
 * Execute the update or delete statement. </p> The semantics are compliant with the ejb3
 * Query.executeUpdate() method.//from  www. j  a  va 2  s  . c  om
 *
 * @param queryName  query name
 * @param paramNames the parameters' names
 * @param values     the parameters' values
 * @return The number of entities updated or deleted.
 */
public int bulkUpdateByNamedQuery(final String queryName, final String[] paramNames, final Object[] values) {
    if (paramNames.length != values.length) {
        throw new IllegalArgumentException("Length of paramNames array must match length of values array"); //$NON-NLS-1$
    }
    Integer deleteCount = execute(new HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            prepareQuery(queryObject);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
                }
            }
            return queryObject.executeUpdate();
        }
    });
    return deleteCount.intValue();
}

From source file:com.model.database.onebusaway.gtfs.hibernate.ext.HibernateOperationsImplExt.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> List<T> findByNamedQueryAndNamedParamsLimited(final String namedQuery, final String[] paramNames,
        final Object[] values, final int limit, final int offset) {
    return (List<T>) execute(new HibernateOperation() {
        public Object doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(namedQuery);
            if (values != null) {
                for (int i = 0; i < values.length; i++)
                    applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
            }//  w  w  w  .  jav a  2s  .com
            queryObject.setFirstResult(offset);
            queryObject.setMaxResults(limit);
            return queryObject.list();
        }
    });
}