Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic bind variables

 
Agasthya Iyer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I am using a named query and in one of the where clause condition I need one or more values in the IN Clause like as follows:

select t.x, t.y from Table t
where t.date between ? and ?
and t.types in (one or more values based on user action).

Is there a way of accomplishing the above without using a Criteria API to build dynamic HQL.

Any help would be greatly appreciated.

Thanks and Regards,
Agasthya
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. There is.
Try giving us some sample code you have written, we might be able to help you further.
 
Agasthya Iyer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I changed my namedQuery to use named parameters like

select t.x, t.y from table t
where t.description = escription
and t.types in (:typesList)

Query query = session.getNamedQuery(queryName);
String[] paramNames = query.getNamedParameters();
if(args.length == paramNames.length) {
for(int i = 0; i < paramNames.length; i++) {
if(paramNames[i].endsWith("List")) {
query.setParameterList(paramNames[i], (List)args[i]);
}else {
query.setParameter(paramNames[i], args[i]);
}
}

where args is an Object[] of the values that I need to bind to the namedParameters in the order in which it is laid out in the Named Query
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic