-->
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.  [ 3 posts ] 
Author Message
 Post subject: Updating a loaded object through HQL not working
PostPosted: Mon Jan 30, 2006 9:30 am 
Newbie

Joined: Mon Jan 30, 2006 9:15 am
Posts: 11
Hy,

I have problems to save (update) a data object which is loaded through HQL. Is it not possible to update a object in the same session?

I used the following code (for testing):

Code:
...
NHibernate.ISessionFactory f = config.BuildSessionFactory();
NHibernate.ISession s = f.OpenSession();
NHibernate.IQuery q = s.CreateQuery("from DoUser");
q.SetMaxResults(1);
DoUser user = (DoUser)q.UniqueResult();
user.LastOnlineDate = DateTime.Now;
s.SaveOrUpdate(user);
...


The log file says:
12:47:50.868 [3400] DEBUG NHibernate.Impl.SessionImpl - Update() object already associated with session

Source-Code SessionImpl.cs
Code:
if( IsEntryFor( theObj ) ) // == true, because the object is already loaded!?
{
   log.Debug( "object already associated with session" );
   // do nothing
}



Hibernate version: 1.0.2


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 3:12 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
If an object is loaded using a query it is considered persistent automatically, so all changes made to it will be written to the database. You don't need to call SaveOrUpdate on it.

In general, it seems to me that you are misunderstanding the way NHibernate works with objects - you don't tell it explicitly to update an object, you just specify which objects you want to be persistent and NH will take care of writing to the database any changes made to those objects.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 3:20 am 
Newbie

Joined: Mon Jan 30, 2006 9:15 am
Posts: 11
Thank you for your comments, sergey ... you are right, i'm completly new to nhibernate ;-)

It's clear now, The data is persisted on "transaction commit" ...

regards, cross


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.