tags:

views:

2365

answers:

3

I could not find any query.executeUpdate() method in IQuery interface or ISession where i can pass an hql to update a record.

here is the hql:

string hql = "update QAD qa set qa.NFS=:NFS where qa.ID = 1145";
IQuery q = session.CreateQuery(hql).SetString("NFS", "10");
+1  A: 

What version are you using? I know it is in 2.1 at least..

Derek Ekins
ah ok, I am using 1.2 :(
dotnetcoder
+1  A: 

I don't think that HQL is meant to support update operations .... When using an O/R mapper, all updates / inserts go through your object - model...
(Even if it is possible to update via HQL, I think (N)Hibernate will retrieve all the objects, modify them, and then save them back to the DB.
In this way , offcourse you loose performance since all objects need to be retrieved).

There also exists a feature request for it; and someone has implemented it, so maybe in a next version ? click

Frederik Gheysels
thanks.I not want to select the object form db and then save it. Rather updated selected columns.Can I use criteria to only update selected columns since already know the data I want to update and the PK ID ?
dotnetcoder
What you can do, is create a native SQL query to do this ... ISQLQuery q = session.CreateSQLQuery( ... ); q.ExecuteUpdate();
Frederik Gheysels
HQL does support update operations in the latest 2.1 beta.
ShaneC
That's what i was saying ... I mentioned: perhas in a future release (I do not consider a beta as a release :) ).Next to that: how is it implemented ? Will it retrieve each and every object that has to be updated ?
Frederik Gheysels
Please update or delete your question (http://meta.stackoverflow.com/questions/11705/how-to-deal-with-obsolete-answers), this has been implemented in 2.1
Mauricio Scheffer
s/question/answer
Mauricio Scheffer
A: 

In NHibernate we are doing it a little bit different: you get the structure from the database, change the value and then commit the changes. I don't think it is possible to do this more directly...

//Pseudocode
T t = session.Get<T>(id);
t.NFS = 10;
session.SaveOrUpdate(t);
session.Transaction.Commit();
bernhardrusch
the downside here is instead of single update statement we end up selecting the entire object from database as the first step. I am wonrdering if I can ignore this here.
dotnetcoder