-->
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.  [ 6 posts ] 
Author Message
 Post subject: Session Save / flush multiple Objects
PostPosted: Mon Dec 20, 2010 12:03 pm 
Newbie

Joined: Fri Feb 05, 2010 8:34 am
Posts: 15
Hi,

this is a very basic question.
I write my problem in pseudo code:

List<Employee> emps = Session.GetAll(); // select via Detached Criteria
emps[0].firstname += "1";
emps[1].firstname += "1";
emps[1].orders[0].shipname += "1";

session.begintransaction()
session.saveorupdate(emps[0]);
session.committransaction();

My intention in code is, to load all Employees, modifiy some data and only save the changes from the first employee.
But NHibernate makes three update Queries, even for the change in the order , where i set the cascade style to "none"

Can someone please explain whats going wrong ?


Top
 Profile  
 
 Post subject: Re: Session Save / flush multiple Objects
PostPosted: Wed Dec 22, 2010 6:56 am 
Newbie

Joined: Tue Dec 07, 2010 10:29 am
Posts: 2
I think that the problem is that saveorupdate is not the method to save in db but simply to manage your entities in NHibernate session. In your case the call it is useless: the persistence is performed by committransaction that call a flush.

I think so, after days of attempts to address the same problem.

I hope that there is a solution.


Top
 Profile  
 
 Post subject: Re: Session Save / flush multiple Objects
PostPosted: Thu Dec 23, 2010 8:05 am 
Newbie

Joined: Fri Feb 05, 2010 8:34 am
Posts: 15
exactly that is my problem.
Flush saves actualy to db and not saveorupdate.

But flush saves all changes made to objects loaded in the same session.
How can i prevent this behaviour /Save only the changes from one object ?


Top
 Profile  
 
 Post subject: Re: Session Save / flush multiple Objects
PostPosted: Thu Feb 10, 2011 4:24 pm 
Newbie

Joined: Fri Feb 05, 2010 8:34 am
Posts: 15
Nobody ?

I think this would help me to understand nhibernate a bit more


Top
 Profile  
 
 Post subject: Re: Session Save / flush multiple Objects
PostPosted: Fri May 06, 2011 4:03 am 
Newbie

Joined: Thu May 05, 2011 1:38 am
Posts: 7
Alternative 1:
Try to evict objects that you dont want to save:
Code:
session.evict(item_not_to_save)

than save your item and attach "item_not_to_save" to your session again.
Its tricky could work, but anyway it is irritating with Hibernate. Image you have a list with 1000+ positions...
sesstion.save(item) and commit should only save "item" not all changes.wrrr... ;-)

Alternative 2:
Create second session "session2" and attach only object that are need to be saved, and save it in session2. Than retach them to your session1. Its tricky also...


Top
 Profile  
 
 Post subject: Re: Session Save / flush multiple Objects
PostPosted: Fri Jun 03, 2011 3:18 am 
Newbie

Joined: Thu May 05, 2011 1:38 am
Posts: 7
Have found another way to do it.
Before saving call session.clear(), so after than only needed objects will be saved:

Code:
obj_A.Txt= updated_description;
obj_B.Txt= updated_description;
         
//clear session cache, so after that only changes in obj_a will be saved,
//otherwise changes in obj_B will be also saved (through trans.commit => session.flush)
session_1.Clear();

//save
using (ITransaction trans = _genDAO.BeginTransaction()) {
   _genDAO.SaveOrUpdate(obj_A);
   trans.Commit();
}


PS. Tested with NHibernate 3.1.0


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