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

The following examples show how to use org.hibernate.Query#setProperties() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: jdal   File: HibernateDao.java    License: 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
@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
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
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
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 Project: lemon   File: HibernateGenericDao.java    License: 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
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
/**
 * 根据查询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;
}