Java Code Examples for org.hibernate.Query#setParameters()

The following examples show how to use org.hibernate.Query#setParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericBaseCommonDao.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 获取分页记录HqlQuery
 * 
 * @param cq
 * @param isOffset
 * @return
 */
@SuppressWarnings("unchecked")
public PageList getPageList(final HqlQuery hqlQuery,
		final boolean needParameter) {

	Query query = getSession().createQuery(hqlQuery.getQueryString());
	if (needParameter) {
		query.setParameters(hqlQuery.getParam(),
				(Type[]) hqlQuery.getTypes());
	}
	int allCounts = query.list().size();
	int curPageNO = hqlQuery.getCurPage();
	int offset = PagerUtil.getOffset(allCounts, curPageNO,
			hqlQuery.getPageSize());
	String toolBar = PagerUtil.getBar(hqlQuery.getMyaction(), allCounts,
			curPageNO, hqlQuery.getPageSize(), hqlQuery.getMap());
	query.setFirstResult(offset);
	query.setMaxResults(hqlQuery.getPageSize());
	return new PageList(query.list(), toolBar, offset, curPageNO, allCounts);
}
 
Example 2
Source File: DBManager.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Hibernate Find method. Use this in a transactional context by using your current transaction object.
 * 
 * @param trx
 *            The current db transaction
 * @param query
 *            The HQL query
 * @param values
 *            The object array containing all search values
 * @param types
 *            The object array containing all Hibernate datatype of the search values
 */
List find(DBTransaction trx, String query, Object[] values, Type[] types) {
    List li = null;
    try {
        long start = 0;
        if (log.isDebugEnabled())
            start = System.currentTimeMillis();
        // old: li = getSession().find(query, values, types);
        Query qu = this.getSession().createQuery(query);
        qu.setParameters(values, types);
        li = qu.list();

        if (log.isDebugEnabled()) {
            long time = (System.currentTimeMillis() - start);
            logQuery("find (time " + time + ", res " + (li == null ? "null" : "" + li.size()) + ")(trans " + trx.hashCode() + ")", values, types, query);
        }

    } catch (HibernateException e) {
        trx.setErrorAndRollback(e);
        String msg = "Find failed in transaction. Query: " + query + " " + e;
        setError(e);
        throw new DBRuntimeException(msg, e);
    }
    return li;
}
 
Example 3
Source File: DBManager.java    From olat with Apache License 2.0 6 votes vote down vote up
int delete(DBTransaction trx, String query, Object[] values, Type[] types) {
    int deleted = 0;

    if (trx.isRolledBack() || trx.isCommitted()) { // some program bug
        throw new DBRuntimeException("cannot delete in a transaction that is rolledback or committed " + values);
    }
    try {
        // old: deleted = getSession().delete(query, values, types);
        Session si = getSession();
        Query qu = si.createQuery(query);
        qu.setParameters(values, types);
        List foundToDel = qu.list();
        deleted = foundToDel.size();
        for (int i = 0; i < deleted; i++) {
            si.delete(foundToDel.get(i));
        }

        if (log.isDebugEnabled()) {
            logQuery("delete (trans " + trx.hashCode() + ")", values, types, query);
        }
    } catch (HibernateException e) { // we have some error
        trx.setErrorAndRollback(e);
        throw new DBRuntimeException("Could not delete object: " + values, e);
    }
    return deleted;
}
 
Example 4
Source File: DBManager.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Hibernate Find method. Use this in a transactional context by using your current transaction object.
 * 
 * @param trx
 *            The current db transaction
 * @param query
 *            The HQL query
 * @param values
 *            The object array containing all search values
 * @param types
 *            The object array containing all Hibernate datatype of the search values
 */
List find(DBTransaction trx, String query, Object[] values, Type[] types) {
    List li = null;
    try {
        long start = 0;
        if (log.isDebugEnabled())
            start = System.currentTimeMillis();
        // old: li = getSession().find(query, values, types);
        Query qu = this.getSession().createQuery(query);
        qu.setParameters(values, types);
        li = qu.list();

        if (log.isDebugEnabled()) {
            long time = (System.currentTimeMillis() - start);
            logQuery("find (time " + time + ", res " + (li == null ? "null" : "" + li.size()) + ")(trans " + trx.hashCode() + ")", values, types, query);
        }

    } catch (HibernateException e) {
        trx.setErrorAndRollback(e);
        String msg = "Find failed in transaction. Query: " + query + " " + e;
        setError(e);
        throw new DBRuntimeException(msg, e);
    }
    return li;
}
 
Example 5
Source File: DBManager.java    From olat with Apache License 2.0 6 votes vote down vote up
int delete(DBTransaction trx, String query, Object[] values, Type[] types) {
    int deleted = 0;

    if (trx.isRolledBack() || trx.isCommitted()) { // some program bug
        throw new DBRuntimeException("cannot delete in a transaction that is rolledback or committed " + values);
    }
    try {
        // old: deleted = getSession().delete(query, values, types);
        Session si = getSession();
        Query qu = si.createQuery(query);
        qu.setParameters(values, types);
        List foundToDel = qu.list();
        deleted = foundToDel.size();
        for (int i = 0; i < deleted; i++) {
            si.delete(foundToDel.get(i));
        }

        if (log.isDebugEnabled()) {
            logQuery("delete (trans " + trx.hashCode() + ")", values, types, query);
        }
    } catch (HibernateException e) { // we have some error
        trx.setErrorAndRollback(e);
        throw new DBRuntimeException("Could not delete object: " + values, e);
    }
    return deleted;
}
 
Example 6
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/**
 * 获取分页记录HqlQuery
 *
 * @param cq
 * @param isOffset
 * @return
 */
@SuppressWarnings("unchecked")
public PageList getPageList(final HqlQuery hqlQuery,
		final boolean needParameter) {

	Query query = getSession().createQuery(hqlQuery.getQueryString());
	if (needParameter) {
		query.setParameters(hqlQuery.getParam(),
				(Type[]) hqlQuery.getTypes());
	}
	int allCounts = query.list().size();
	int curPageNO = hqlQuery.getCurPage();
	int offset = PagerUtil.getOffset(allCounts, curPageNO,
			hqlQuery.getPageSize());
	String toolBar = PagerUtil.getBar(hqlQuery.getMyaction(), allCounts,
			curPageNO, hqlQuery.getPageSize(), hqlQuery.getMap());
	query.setFirstResult(offset);
	query.setMaxResults(hqlQuery.getPageSize());
	return new PageList(query.list(), toolBar, offset, curPageNO, allCounts);
}