Tuesday, August 31, 2010

Name Parameter in HQL of Hibernate

Name Parameter in HQL is nice, like the parameter statement in sql, it is more robust than build the hql in flight and for the special characters. But it is also a little picky. It took me about 30 minutes to figure out what is wrong and the correct combination.


Session session=getSession();
String hql="from Job where (ownerId=:userId) AND ((name LIKE :keyword) OR (description LIKE :keyword) "
+" OR (flow.name LIKE :keyword) OR (flow.description LIKE :keyword)"
+"OR (flow.typeName LIKE :keyword) OR (flow.keyWords LIKE :keyword))";
Query query=session.createQuery(hql);
query.setLong("userId", userId).setString("keyword", "%"+keyword+"%");


You cannot put flow.keyWords LIKE '%:keyword%' or flow.keyWords LIKE %:keyword% in the hql, it has to go in the setString().

No comments :