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

The following examples show how to use org.hibernate.Query#setProperties() . 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: HibernateDao.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * Get Hibernate named Query and configure with filter from page. 
 * Set the result count on page also. 
 * @param page page 
 * @return Hibernate named Query.
 */
private Query getQuery(Page<?> page) {
	Object filter = page.getFilter();
	
	try {
		if (filter instanceof Filter) {
			Filter f = (Filter) filter;
			Query query = getSession().getNamedQuery(f.getFilterName());
			Query countQuery = getSession().createQuery(query.getQueryString().replaceFirst("select", "count"));
			query.setProperties(f.getParameterMap());
			query.setMaxResults(page.getPageSize());
			query.setFirstResult(page.getStartIndex());
			page.setCount((Integer) countQuery.uniqueResult());

			return query;
		}
	}
	catch (HibernateException e) {}
	return null;
}
 
Example 2
Source File: SimpleHibernateDao.java    From DWSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Query createQuery(final String queryString, final Map<String, ?> values) {
	AssertUtils.hasText(queryString, "queryString不能为空");
	Query query = getSession().createQuery(queryString);
	if (values != null) {
		query.setProperties(values);
	}
	return query;
}
 
Example 3
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetProperties() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(simple);
	assertTrue( q.list().get(0)==simple );
	//misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
	Single single = new Single() { // trivial hack to test properties with arrays.
		String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); }
	};

	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	single.setSeveral(l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );


	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 4
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetPropertiesMap() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Map parameters = new HashMap();
	parameters.put("name", simple.getName());
	parameters.put("count", new Integer(simple.getCount()));
	
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(((Map)parameters));
	assertTrue( q.list().get(0)==simple );
	
	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	parameters.put("several", l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(parameters);
	assertTrue( q.list().get(0)==simple );


	parameters.put("stuff", l.toArray(new String[0]));
	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(parameters);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 5
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetProperties() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(simple);
	assertTrue( q.list().get(0)==simple );
	//misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
	Single single = new Single() { // trivial hack to test properties with arrays.
		String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); }
	};

	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	single.setSeveral(l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );


	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 6
Source File: HibernateGenericDao.java    From lemon with Apache License 2.0 5 votes vote down vote up
/**
 * create query by hql and map.
 * 
 * @param hql
 *            String
 * @param map
 *            Map
 * @return Query
 */
public Query createQuery(String hql, Map<String, Object> map) {
    Assert.hasText(hql, "hql cannot be null");

    Query query = this.getSession().createQuery(hql);

    if (map != null) {
        query.setProperties(map);
    }

    return query;
}
 
Example 7
Source File: SQLLoaderTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
public void testFindBySQLProperties() throws HibernateException, SQLException {
	Session session = openSession();
	session.delete("from Category");

	Category s = new Category();
	s.setName(String.valueOf(nextLong++));
	session.save(s);

	s = new Category();
	s.setName("WannaBeFound");
	session.flush();

	Query query = session.createSQLQuery("select {category.*} from category {category} where {category}.name = :name", "category", Category.class);

	query.setProperties(s);
	//query.setParameter("name", s.getName());

	query.list();

	query = session.createSQLQuery("select {category.*} from category {category} where {category}.name in (:names)", "category", Category.class);
	String[] str = new String[] { "WannaBeFound", "NotThere" };
	query.setParameterList("names", str);
	
	query.uniqueResult();
	
	session.connection().commit();
	session.close();
	
	

}
 
Example 8
Source File: BasicHibernateDao.java    From base-framework with Apache License 2.0 3 votes vote down vote up
/**
 * 根据查询HQL与参数列表创建Query对象
 * 
 * @param queryOrNamedQuery hql 或者Hibernate的NamedQuery
 * @param values
 *            命名参数,按名称绑定.
 *            
 * @return {@link Query}           
 * 
 */
protected Query createQuery( String queryOrNamedQuery, Map<String, ?> values) {
	Query query = createQuery(queryOrNamedQuery);
	if (values != null) {
		query.setProperties(values);
	}
	return query;
}