1

I am trying to write a generic method that can pick up a named query, set the named parameters in it and return a result.

The method looks like the following,

s = getSession();
q = s.getNamedQuery(nameOfTheQuery);
keySet = queryParams.keySet();
itr = keySet.iterator();
while(itr.hasNext()){
    key = itr.next();
    //Problem here
    q.setParameter(key, queryParams.get(key));
    }
q.setMaxResults(maxResults);
q.setFetchSize(fetchSize);
log.info("::>>>> Query result :"+(q.uniqueResult()));

I am trying to set the named parameters to values here. But when the parameter here happens to be a list or collection I get a ClassCastException while the q.uniqueResult()

Is there a way I can write this method to support collections and other types of parameters as well? It is mandatory that I set the maxResults and fetchSize so I had to choose this option. Any help would be greatly appreciated. Thanks!

1
  • If you want a proper answer to this question I believe you need to put in the full code of the entire method (including method signature). It's hard to follow. Also, since you said to Naved "I wanted to know how to determine the type of the paramater or some mechanism to find that and change the method accordingly" you should put that information somewhere in your question. Commented Oct 24, 2013 at 0:44

3 Answers 3

2

You need to use setParameterList(key, value) where vale is your list.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, in other words how do I know if the parameter is a list or what type it is?
if (queryParams.get(key) instanceOf(List)) setParameterList()
Sorry, you would actually want to more generally check value instanceOf Collection
This seems to be the right answer to the question. I don't get why the other answer gets so many votes ...
1

I suspect the answer to your question is firstly to use the setParameterList method when the parameter is a list type; secondly by using one of the following techniques:

  1. Use reflection to interrogate the type of your parameters and then use setParameter or setParameterList method accordingly.

  2. Use the benefits of polymorphism to capture parameters that are List objects and call setParameterList for those. Example below.*

  3. Make a big conditional block that tests casting to a bunch of list types and if it casts then call setParameterList, otherwise call setParameter.

(*) Example for approach 2.

while(itr.hasNext()) 
{
    key = itr.next();
    QueryParameterHelper.setGenericParameter(q, key, queryParams.get(key));
}

public static class QueryParameterHelper
{
    public static void setGenericParameter(Query query, String paramName, List listValue)
    {
        query.setParameterList(paramName, listValue);   
    }

    public static void setGenericParameter(Query query, String paramName, String stringValue)
    {
        query.setParameter(paramName, stringValue);
    }

    //etc for other possible parameter types
}

Comments

0

If I understand your question correctly.

In my case I often use q.getResultList to get the collection of the result.
I think this may help you to find the solution.

2 Comments

Sorry Naved...I wanted to know how to determine the type of the paramater or some mechanism to find that and change the method accordingly
It's because your answer is wrong, but the answer "setParameterList(key, value)" is right according to me. So I upvoted that answer. It's as simple as that. But I appreciate that you're trying to help of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.