Reputation: 26212
Hiya, I'm sort a newbie when it comes to hibernate so I'm going to try to ask my question as clearly as possible. I wanna execute following query as hibernate query .
<sql-query name="updateINFO">
update
info
set
status_id = 2
where
pid = :i
</sql-query>
Now i is a dynamic value, so sometimes I'll pass 1 sometimes 1000, actually I'll iterate trough list and do the query for every item in the list, you get my point. Now here is my part of java calling execution of this query ..
for(int i=0; i<list.size(); i++)
{
Query query = session.getNamedQuery("updateINFO").setParameter("pid", list.get(i));
}
Is there something wrong with this approach ? thank you
I already have created list of type Long and I get list items from another query
List<Long> list = null;
Query query = session.getNamedQuery("endDateChecker");
list = query.list();
here is my method :
public List<Long> I need findItemByPIdEndDate() throws Exception {
List<Long> list = null;
try{
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("endDateChecker");
list = query.list();
for (Long listItem : list) {
Query query1 = session.getNamedQuery("updateINFO")
.setParameter("in",listItem);
}
}catch (HibernateException e){
throw new DataAccessException(e.getMessage());
}
return list;
}
Upvotes: 1
Views: 2462
Reputation: 8783
If you're having trouble using the NHibernate Query Language, they offer an alternative known as Criteria Querying. The API allows you to build your query strings more dynamically and the is also more extensible than HQL. Give it a shot and see if it suits your needs.
Upvotes: 0
Reputation: 11338
You could use setParameterList:
<sql-query name="updateINFO">
update info set status_id = 2 where pid in (:parameters)
</sql-query>
public List <Long> findItemByPIdEndDate() throws Exception {
List<Long> list = null;
try{
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("endDateChecker");
list = query.list();
Query query1 = session.getNamedQuery("updateINFO")
.setParameterList("parameters",list);
query1.executeUpdate();
}catch (HibernateException e){
throw new DataAccessException(e.getMessage());
}
return list;
}
Upvotes: 2
Reputation: 36987
From your original question:
for(int i=0; i<list.size(); i++)
{
Query query = session.getNamedQuery("updateINFO").setParameter("pid", list.get(i));
}
I think you forgot a call to the query.executeUpdate()
method...
Upvotes: 1