tags:

views:

319

answers:

3

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;
    }
+1  A: 
marcos
I've tried setParameterList and it gives me compile error, since my list is long type.
Gandalf StormCrow
You cant create a primitive type Collection. use the Long class instead.
marcos
I'm not sure what do I need to do with that but here is the compile error "The method setParameterList(String, Collection) in the type Query is not applicable for the arguments (String, Long)"
Gandalf StormCrow
I edited my question so that you see I already have a list
Gandalf StormCrow
Oh i see what you are doing, you have to pass your list as an argument to the set parameter list.
marcos
Exactly what I'm trying to do :)
Gandalf StormCrow
well the error you're getting says you're passing a Long instead of a Collection.
marcos
So what do I do ?
Gandalf StormCrow
Could you show me how exactly you are calling the setParameterList method?
marcos
Sure I'll edit the question again .. in a minute or so
Gandalf StormCrow
I'm trying to build it and run .. I'll get back to you
Gandalf StormCrow
Hmm its not working, and its not trowing any exeptions, I just added a syso line to print out hellou when it executes .. it executes but for some twister weird reason I have no idea of .. its not working , anymore ideas ?
Gandalf StormCrow
Just like ammoQ said, you also forgot to call query1.executeUpdate()
marcos
+1  A: 

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

ammoQ
Thank you ammoQ .. I had to accept marco's solution .. but yours was final part of the puzzle .
Gandalf StormCrow
Gandalf: sure, it's more an additional hint than the "answer" to the question
ammoQ
A: 

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.

James Jones