These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Partial Update
PostPosted: Wed Aug 13, 2008 10:51 am 
Newbie

Joined: Wed Aug 13, 2008 10:00 am
Posts: 7
Hi,

I am on a situation where I get the Id of a row and I would like to update only 2 columns of that row. So my question is :
Is it possible to do a partial update on an object without using native SQL (CreateSQLQuery)?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 11:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
There is a dynamic-update attribute on the class mapping:

Quote:
dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.


But I'm not sure if it's yet implemented. Maybe in 2.0.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 11:35 am 
Newbie

Joined: Wed Aug 13, 2008 10:00 am
Posts: 7
That works but only if I load the object first, so then it only updates the dirty properties.
What I want is something like "update X set x1 = :x1, x2 = :x2 where Id = :id" without having to load X with Id = :id first.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 5:41 pm 
Newbie

Joined: Thu Jun 28, 2007 5:02 pm
Posts: 12
To clarify, are you trying to get NHibernate to generate SQL through HQL without having to load an object?

I haven't tested this to see if NHibernate hooks into the session cache for any of the methods I'm calling below (if it does, odds are, some sort of hydration is occurring, and that's what you're trying to avoid)



Where x is an open ISession object...

string aSQLString = x.GetSessionImplementation().GetQueries(anHQLQueryString, false)[0].SQLString

GetQueries returns the SQL queries generated by NHibernate to perform the HQL. Depending upon the query, there might be one or more queries performed to resolve the desired result-set.

My example above will show the first query it wants to perform (hence the [0] index reference to the translator array). You could loop through the translators and perform a CreateSQLQuery on the session object for each of those raw sql strings and execute them in order.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 7:02 pm 
Newbie

Joined: Wed Aug 13, 2008 10:00 am
Posts: 7
I know I can do what I want with CreateSQLQuery. But that means that the sql is Database specific.
I would like to do CreateQuery("update X set ....").ExecuteUpdate() but this doesn't work because CreateQuery expects SELECT or FROM.
I must be missing something.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 1:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Which version do you use ? I think Nhibernate 2.0 now supports:

IQuery.ExecuteUpdate()

But I haven't had a closer look at it, I'm still bound to 1.2.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 2:34 am 
Newbie

Joined: Mon Feb 18, 2008 3:35 pm
Posts: 12
Location: Belarus
For detached Entities you can implement int[] IInterceptor.FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
return null;
}
of the session interceptor, so Session will know about fields wich was changed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 12:48 pm 
Newbie

Joined: Thu Jun 28, 2007 5:02 pm
Posts: 12
The example I provided translates HQL to the raw sql of the database environment your session configuration is targetting. The raw sql you get will differ from database to database, so the translation that's occurring doesn't make it database specific.

However, the only way to get that generated raw SQL to execute is to throw it directly at the database server (via the CreateSQLQuery strategy)


You are correct. I just tested 1.2 and it throws an exception when you try to create an update/insert/delete HQL query with CreateQuery(). Sorry about that.

2.0 must have a new translator that supports the update/insert/delete HQL.

If 2.0 is an option for you, you might want to look into the command that wolli's mentioned. I believe it's designed for bulk operations and attempts to improve performance in the way you're desiring.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 3:48 pm 
Newbie

Joined: Wed Aug 13, 2008 10:00 am
Posts: 7
I'm using 2.0 CR1 and I still can't do updates with HQL. (throws exception saying that CreateQuery expects SELECT or FROM)
I'll try your strategy and see if I can work it out.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.