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.  [ 2 posts ] 
Author Message
 Post subject: "Unit of work" i can't uderstand
PostPosted: Thu Mar 06, 2008 6:53 am 
Newbie

Joined: Mon Feb 18, 2008 3:35 pm
Posts: 12
Location: Belarus
Example1:

ISession s = SessionFactory.OpenSession();
//I don't know witch flush mode should i use
IList<MyEntity> l = s.CreateQuery("FROM MyEntity").List<MyEntity>();
l[0].Something.Something.Name += "xxx";
s.SaveOrUpdateCopy(l[0].Something.Something);
s.BeginTransaction();
l[0].Quantity += 16;
s.SaveOrUpdateCopy(l[0]);
s.Flush();
s.Transaction.Commit();
s.Disconnect();

Then if we look at the queries to DB we will see:
SELECT ...
SELECT...
BEGIN TRANSACTION
UPDATE MyEntity // Where is the logic? this must be befor transaction, so in case of ROLLBACK chenges must apply
UPDATE SomeEntity
COMMIT

Example2:

ISession s = SessionFactory.OpenSession();
//I don't know witch flush mode should i use
IList<MyEntity> l = s.CreateQuery("FROM MyEntity").List<MyEntity>();
l[0].Something.Something.Name += "xxx";
//s.SaveOrUpdateCopy(l[0].Something.Something);// - THIS LINE IS COMMENTED !!! IMPORTANT
s.BeginTransaction();
l[0].Quantity += 16;
s.SaveOrUpdateCopy(l[0]);
s.Flush();
s.Transaction.Commit();
s.Disconnect();

Then if we look at the queries to DB we will see:
SELECT ...
SELECT...
BEGIN TRANSACTION
UPDATE MyEntity // OBJECT saved!!!! I do not want this!! i want to save only objects i explicitly call SaveorUpdate or cascades of it!!!!
UPDATE SomeEntity
COMMIT


Can any body explain me this situations?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 7:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
session.Save/Update/SaveOrUpdate is for objects, that are NOT already in the session. When you modify objects that are in your current session and flush this session, your changes will be written to the database.

You have to close your session and use a new one for saving the objects you want.

Btw. s.Flush() is done in s.Transaction.Commit(). It makes no sense to flush the session before.

_________________
--Wolfgang


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