2

I need to write a general function that handle queries. the function get List<String> that hold parameters that is sent to the query. how i implement that in hibernate.

to be more particular, if my query is:

select * from people where name=:name and family=:family

my list will contain

<"name","myname">
<"family","myfamily">
1
  • better way to use queryString Commented Jun 21, 2011 at 10:00

5 Answers 5

1

getHibernateTemplate() is most probably returning a org.springframework.orm.hibernate3.HibernateTemplate which is a Hibernate helper class in the spring framework. This is the code from the find method called therein:

    public List find(final String queryString, final Object[] values) throws DataAccessException {
    return (List) executeWithNativeSession(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery(queryString);
            prepareQuery(queryObject);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    queryObject.setParameter(i, values[i]);
                }
            }
            return queryObject.list();
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at the Hibernate Criteria API

This should get you started.

1 Comment

thanks, the problem is that i can't build the sql with creteria, because all the sql is passed as param to the function.
0

write query as:

String str="select * from people where name=? and family=?";
List<Object> queryParam=new ArrayList<Object>();
queryParam.add(name); //queryParam.add(yourList.get(0));
queryParam.add(family); //queryParam.add(yourList.get(1));
List<Object[]> List=getHibernateTemplate().find(str,queryParam.toArray());

2 Comments

Thanks for the answer, i understand that i should creat a list that has a param name followed by it's value. i didn't understand what is the line with getHibernateTemplate
It will return HibernateTemplate we can say that this is a helper class which will execute your query and return objects.
0

If I read your question correctly, you want a generalized way to call a query that used named parameters, passing in a list that contains the the name/value pairs to bind.

This is pretty simple. I'm omitting the use of generics for clarity.

    String query="select p from Person p where name=:name and family=:family";
    String[] parameterArray = new String[] {"name", "myName", "family","myFamily"}; 
    List parameters = Arrays.asList(parameterArray);
    List results = callQuery(session, query, parameters);

public List callQuery(Session session, String query, List parameters) {
      Query query = session.createQuery(query);
      if (parameters != null && !parameters.isEmpty()) {
          for (int i = 0; i < parameters.size(); i+=2) {
            query.setParameter(parameters[i],parameters[i+1]);
          }
      }
      return query.list();
    }

An alternative is to use a Map, which makes iterating through the parameters to add them to the query a little cleaner; I also find it makes adding them to the collection to pass in to the generic method clearer, as well:

    String query="select p from Person p where name=:name and family=:family";
    Map<String,Object> parameters = new HashMap<String,Object>();
    parameters.put("name","myName");
    parameters.put("family,"myFamily");
    List results = callQuery(session, query, parameters);

public List callQuery(Session session, String query, Map<String,Object> parameters) {
      Query query = session.createQuery(query);
      if (parameters != null && !parameters.isEmpty()) {
          for (Map.Entry<String,Object> entry : parameters) {
            query.setParameter(entry.getKey(),entry.getValue());
          }
      }
      return query.list();
    }

I'd also recommend looking into NamedQueries over the use of inline strings, as they let you include the queries in your mapping xml/annotations and add some query syntax validation.

Comments

0

You can use parameter list to inlcude in your query with 'IN' and 'setParameterList'

List<String> yourListString = new ArrayList<String>();

Query query = getSession().createQuery("select * from people where name in (:yourListString) and family in (:yourListString)"
query.setParameterList("yourListString ", yourListString);
query.executeUpdate();

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.