my dog learned polymorphism
The moose likes Object Relational Mapping and the fly likes Dynamic bind variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Dynamic bind variables" Watch "Dynamic bind variables" New topic

Dynamic bind variables

Agasthya Iyer

Joined: Sep 22, 2005
Posts: 17
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 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,

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 661

Yes. There is.
Try giving us some sample code you have written, we might be able to help you further.

If you are not laughing at yourself, then you just didn't get the joke.
Agasthya Iyer

Joined: Sep 22, 2005
Posts: 17
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
I agree. Here's the link:
subject: Dynamic bind variables
It's not a secret anymore!