Example usage for org.hibernate Query setProperties

List of usage examples for org.hibernate Query setProperties

Introduction

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

Prototype

Query<R> setProperties(Map bean);

Source Link

Document

Bind the values of the given Map for each named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.

Usage

From source file:org.opengoss.dao.hibernate.DataAccessor.java

License:Apache License

public List findByValueBean(final String queryString, final Object valueBean) throws DaoException {

    return (List) execute(new IAccessorCallback() {
        public Object call(Session session) throws HibernateException {
            Query queryObject = session.createQuery(queryString);
            queryObject.setProperties(valueBean);
            return queryObject.list();
        }//ww  w  . j  av  a2  s .c o  m
    });
}

From source file:org.opengoss.dao.hibernate.DataAccessor.java

License:Apache License

public List findByNamedQueryAndValueBean(final String queryName, final Object valueBean) throws DaoException {

    return (List) execute(new IAccessorCallback() {
        public Object call(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            queryObject.setProperties(valueBean);
            return queryObject.list();
        }//from  w  ww. j  a  v  a 2 s. co m
    });
}

From source file:org.riotfamily.core.dao.hibernate.AbstractHqlDao.java

License:Apache License

@SuppressWarnings("unchecked")
protected void setFilterParameters(Query query, ListParams params) {
    if (params.getFilter() instanceof Map) {
        Map<String, ?> filterMap = (Map<String, ?>) params.getFilter();
        for (Map.Entry<String, ?> entry : filterMap.entrySet()) {
            String name = entry.getKey();
            HibernateUtils.setParameter(query, name.replaceAll("\\.", "_dot_"), entry.getValue());
        }/*from w  w w  .  j  av a2  s .  c  o m*/
    } else {
        query.setProperties(params.getFilter());
    }

    HqlUtils.setCollectionValueParams(query, params.getFilteredProperties(), getEntityClass(),
            params.getFilter());
}

From source file:org.springframework.batch.item.database.HibernateItemReaderHelper.java

License:Apache License

/**
 * Get a cursor over all of the results, with the forward-only flag set.
 *
 * @param fetchSize the fetch size to use retrieving the results
 * @param parameterValues the parameter values to use (or null if none).
 *
 * @return a forward-only {@link ScrollableResults}
 *//*w ww  .j av a2  s .c o m*/
public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) {
    Query query = createQuery();
    if (parameterValues != null) {
        query.setProperties(parameterValues);
    }
    return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY);
}

From source file:org.springframework.batch.item.database.HibernateItemReaderHelper.java

License:Apache License

/**
 * Read a page of data, clearing the existing session (if necessary) first,
 * and creating a new session before executing the query.
 *
 * @param page the page to read (starting at 0)
 * @param pageSize the size of the page or maximum number of items to read
 * @param fetchSize the fetch size to use
 * @param parameterValues the parameter values to use (if any, otherwise
 * null)// w  w  w.j  a va 2  s  .  c  o m
 * @return a collection of items
 */
public Collection<? extends T> readPage(int page, int pageSize, int fetchSize,
        Map<String, Object> parameterValues) {

    clear();

    Query query = createQuery();
    if (parameterValues != null) {
        query.setProperties(parameterValues);
    }
    @SuppressWarnings("unchecked")
    List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize)
            .list();
    return result;

}

From source file:org.springframework.orm.hibernate3.HibernateTemplate.java

License:Apache License

@Override
public List<?> findByValueBean(final String queryString, final Object valueBean) throws DataAccessException {

    return executeWithNativeSession(new HibernateCallback<List<?>>() {
        @Override//  w  ww. j av a 2s . c  o  m
        @SuppressWarnings("unchecked")
        public List<?> doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery(queryString);
            prepareQuery(queryObject);
            queryObject.setProperties(valueBean);
            return queryObject.list();
        }
    });
}

From source file:org.springframework.orm.hibernate3.HibernateTemplate.java

License:Apache License

@Override
public List<?> findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
        throws DataAccessException {

    return executeWithNativeSession(new HibernateCallback<List<?>>() {
        @Override//w w  w  .ja va  2s . co m
        @SuppressWarnings("unchecked")
        public List<?> doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            prepareQuery(queryObject);
            queryObject.setProperties(valueBean);
            return queryObject.list();
        }
    });
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testFindByValueBean() throws HibernateException {
    Query query = mock(Query.class);
    TestBean tb = new TestBean();
    List list = new ArrayList();
    given(session.createQuery("some query string")).willReturn(query);
    given(query.setProperties(tb)).willReturn(query);
    given(query.list()).willReturn(list);
    List result = hibernateTemplate.findByValueBean("some query string", tb);
    assertTrue("Correct list", result == list);
    verify(query).setProperties(tb);//w  ww.j a  v a 2  s  . c  o  m
    verify(session).flush();
    verify(session).close();
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testFindByNamedQueryAndValueBean() throws HibernateException {
    Query query = mock(Query.class);
    TestBean tb = new TestBean();
    List list = new ArrayList();
    given(session.getNamedQuery("some query name")).willReturn(query);
    given(query.setProperties(tb)).willReturn(query);
    given(query.list()).willReturn(list);
    List result = hibernateTemplate.findByNamedQueryAndValueBean("some query name", tb);
    assertTrue("Correct list", result == list);
    verify(query).setProperties(tb);/*from w  w w  .  j av a2  s .co  m*/
    verify(session).flush();
    verify(session).close();
}

From source file:org.springframework.orm.hibernate3.StatelessHibernateTemplate.java

License:Apache License

public List findByValueBean(final String queryString, final Object valueBean) throws DataAccessException {

    return (List) execute(new CombinedHibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            return executeQuery(session.createQuery(queryString));
        }// w w w  .  ja  v a 2 s .  c o m

        public Object doInHibernate(StatelessSession session) throws HibernateException {
            return executeQuery(session.createQuery(queryString));
        }

        private List executeQuery(Query queryObject) {
            prepareQuery(queryObject);
            queryObject.setProperties(valueBean);
            return queryObject.list();
        }
    }, true);
}