开发者

hibernate list of parameters

开发者 https://www.devze.com 2023-03-14 03:33 出处:网络
I need to write a general function that handle queries. the function get List<String> that hold parameters that is sent to the query.

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">


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();
        }
    });
}


Have a look at the Hibernate Criteria API

This should get you started.


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());


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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消