1

I have created a generic method which will take hibernate named query, a HashMap object which contain parameter name values for that named query in the following manner:

The method signature is:

public static <T> ArrayList<T> getData(String hibernateQueryName, HashMap<?, ?> 
                                             queryNameValue) throws Exception;

Now suppose I have a SQL and its hibernateQueryName is empData:

SELECT e.emp_nm      AS EMP_NAME
     , e.emp_address AS EMP_ADDR
  FROM employee e
 WHERE e.emp_no > :empNumber
   AND e.name LIKE :empName

Here empNumber and empName are parameter name and they are of type Integer and String respectively.

Now suppose I have one more SQL and its hibernateQueryName is studentData:

SELECT s.s_nm    AS EMP_NAME
  FROM student s
 WHERE s.dob > :studentDOB

Here studentDOB is parameter name and its of type Date. studentDOB is a key that I will get from HashMap.

I want to set these parameter and values to query object like:

Query query = getSession().getNamedQuery(hibernateQueryName);
query.setXXX(parameterName, parameterValue);

Above code is written in my getData method.

There are many query.setXXX methods which are based on Type for example query.setInteger() but for calling this I need to find the type of ? which I am passing in getData method. Is it possible to use query.setXXX method with unknown type ?

2
  • I think you may need to rewrite your question for clarification. Are you asking how to use your getData parameters inside of a query? Commented Apr 9, 2014 at 12:56
  • @JordanD I have edited my question. Please let me know if I was able to clarify my point. Commented Apr 9, 2014 at 14:21

2 Answers 2

1

Try using query.setParameter("name", value) this does not expect an explicit type.

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

2 Comments

Thanks. the value is of type Object does hibernate takes care of type before using it in named query as in query we may have different types like Date, String, Int...?
I would recommend trying it and if you run into another specific problem edit your original question. hibernate will take care of types as long as you do not try to cast incompatible types.
1

The signature of query.set parameter is

void QueryInstance.setParameter(String, Object);

You may use HashMap<String,Object> as parameter, iterate it in method and assign as per the mentioned method. It will act as generic. I have made generic that way, it may help in your case as well.

Comments

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.