-->
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.  [ 14 posts ] 
Author Message
 Post subject: SaveOrUpdateCopy flushes directly, even with FlushMode.Never
PostPosted: Fri Aug 08, 2008 5:19 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
Hibernate version: NHibernate 2.0


I have something that I do not expect.
I have the following code:

Code:
Configuration cfg = new Configuration();
cfg.Configure();

ISessionFactory f =  cfg.BuildSessionFactory();

ISession s = f.OpenSession (new AuditInterceptor());
s.FlushMode = FlushMode.Never;

User p = new User ();
p.Name = "melperz0er";
p.IsAdministrator = true;
p.EmailAddress = "@dkdke";

s.SaveOrUpdateCopy (p);

As you can see, I simply create a Session, and set its FlushMode to Never.
This means -according to the documentation- that the changes should only be flushed when I explicitly call the Flush method of the Session.

However, as I was debugging this testcase, I noticed that calling SaveOrUpdateCopy on the session, immediatly resulted in the execution of an INSERT query. (I've seen this via SQL Server profiler).
Also, I do not flush the session, so in fact, the new User record should never have been inserted.

Am I doing something wrong ?
Is there something in the documentation that I haven't seen ?
Is this a feature ? Is this by design ? Is it a bug ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 5:35 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What kind of generator class for the id do you use ? If you use a generator that uses db generated ids (identity, native, ...), you'll get an immediate insert to retrieve the id for the new object.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 6:33 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
wolli wrote:
What kind of generator class for the id do you use ? If you use a generator that uses db generated ids (identity, native, ...), you'll get an immediate insert to retrieve the id for the new object.


I use the 'identity' generator, so indeed, that is a generator which uses the DB.
But, this is quite confusing, when you specifically set your flushmode to never .


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 7:24 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hibernate assignes/generates the id to the object when you Save() it to the session. It has to because internally hibernate handles the objects in an identity map. So if you use such generators the insert is the only way to obtain the id. Have a look at this thread on the nhusers list at google groups:

http://groups.google.com/group/nhusers/browse_thread/thread/12b1449ea0cf52a5/e48d7397d4980179?hl=en

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 2:58 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
I also have another situation:

I have an entity for which i use the 'guid' generator. My FlushMode is set to 'Auto'.
When I call SaveOrUpdateCopy, I see that NHibernate performs a SELECT, but he doesn't perform an update (although i'm sure my entity has changed).
When I execute 'Flush', then the Update is executed. Strange, since now I find that the Update should be executed at the point where I call SaveOrUpdateCopy, isn't it ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 3:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
No, except the case with generated ids, updates and inserts does not happen until the session is flushed (either explicitly or implicitly by committing the transaction).

Flush mode auto just means that hibernate flushes the session when it needs to.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 4:38 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
wolli wrote:
No, except the case with generated ids, updates and inserts does not happen until the session is flushed (either explicitly or implicitly by committing the transaction).

Flush mode auto just means that hibernate flushes the session when it needs to.

Well, in my opinion, it would then be better to ditch the public FlushMode property, since:
- FlushMode auto means that hibernate flushes when it needs to (ok)
- FlushMode never means that hibernate flushes as well when it needs to (to acquire the id of an entity when db-generators are used).

IMHO , this only adds confusion.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 6:52 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You may want to join the discussion on the nhusers group (link above). There's a in deep discussion of this.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: update of guid ID columns
PostPosted: Mon Aug 25, 2008 7:06 pm 
Newbie

Joined: Mon Aug 25, 2008 6:51 pm
Posts: 3
hello, i am running the same problem of whoami's, when try to update a table with a guid as primary key ( id ).

here is my save button code

ISession session = NHibernateHttpModule.CurrentSession;
ITransaction transaction = session.BeginTransaction();


ProfileTbl profile;

if (isNew)
{ profile = new ProfileTbl();
}
else
{

profile = (ProfileTbl)session.Load(typeof(ProfileTbl), companyId);
}


profile.CompanyName = txtName.Text;
profile.Phone1 = txtPhone1.Text;
profile.Phone2 = txtPhone2.Text;
profile.Fax = txtFax.Text;
profile.Address1 = txtAddress1.Text;
profile.Address2 = txtAddress2.Text;
profile.City = TxtCity.Text;
profile.State = txtState.Text;
profile.Zip = txtZip.Text;
profile.ShowItemCost = chkItemCost.Checked;
profile.ShowOutstandingBalance = chkOutstandingBalance.Checked;
profile.PrintSignature = chkPrintSignature.Checked;

session.SaveorUpdate(profile);

}

session.Flush();
transaction.Commit();
session.Close();

session.Dispose();

The insert works but the update does nothing, also in the log file, i see this long set of entries, but dont see any helpful

16:46:10 [8] DEBUG NHibernate.Impl.Printer - listing entities:
16:46:10 [8] DEBUG NHibernate.Impl.Printer - listing entities:
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=False, Fax= , City=, CompanyId=ce66ec3b-d381-4618-b474-41035c2c5eb9, Address2=, ShowItemCost=False, ShowOutstandingBalance=False, CompanyName=otra empresa}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=False, Fax= , City=, CompanyId=ce66ec3b-d381-4618-b474-41035c2c5eb9, Address2=, ShowItemCost=False, ShowOutstandingBalance=False, CompanyName=otra empresa}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=True, Fax= , City=, CompanyId=a97441fb-3e87-4dce-be45-6757cb65b81f, Address2=, ShowItemCost=False, ShowOutstandingBalance=True, CompanyName=uno dos tres cuatro}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=True, Fax= , City=, CompanyId=a97441fb-3e87-4dce-be45-6757cb65b81f, Address2=, ShowItemCost=False, ShowOutstandingBalance=True, CompanyName=uno dos tres cuatro}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=False, Fax= , City=, CompanyId=40e75238-3485-4ded-977a-5f00d651c057, Address2=, ShowItemCost=False, ShowOutstandingBalance=False, CompanyName=tres cuatro cinco}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1= , Address1=, Phone2= , Zip=, CloseJobStatus=null, PrintSignature=False, Fax= , City=, CompanyId=40e75238-3485-4ded-977a-5f00d651c057, Address2=, ShowItemCost=False, ShowOutstandingBalance=False, CompanyName=tres cuatro cinco}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1=813.240.7772, Address1=P.O. Box 549, Phone2=, Zip=, CloseJobStatus=Closed, PrintSignature=False, Fax=813.671.5562, City=http://www.savageconsulting.net, CompanyId=836fa1b6-1190-4005-b434-7ed921be2026, Address2=Riverview, Fl. 33568, ShowItemCost=True, ShowOutstandingBalance=False, CompanyName=Savage Consulting, Inc.}
16:46:10 [8] DEBUG NHibernate.Impl.Printer - DataLayer.DataClases.ProfileTbl{State=, Phone1=813.240.7772, Address1=P.O. Box 549, Phone2=, Zip=, CloseJobStatus=Closed, PrintSignature=False, Fax=813.671.5562, City=http://www.savageconsulting.net, CompanyId=836fa1b6-1190-4005-b434-7ed921be2026, Address2=Riverview, Fl. 33568, ShowItemCost=True, ShowOutstandingBalance=False, CompanyName=Savage Consulting, Inc.}
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - opened session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - flushing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - executing flush
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - post flush
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.SessionImpl - closing session
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
16:46:10 [8] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 2:13 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is that the log during the flush or the commit ? The flush at that point isn't necessary (if not wrong):

session.Flush();
transaction.Commit();


The session is flushed during the commit.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: is all the generated log
PostPosted: Tue Aug 26, 2008 2:18 am 
Newbie

Joined: Mon Aug 25, 2008 6:51 pm
Posts: 3
Hello.

Thanks for your reply.

That is the full log i see in the file.

for the flush and commit instructions, i had tried all the ways, only flush, only commit, first flush then commit, first commint then flush, but nothing works, the update doesnt change the data in the sqlserver.

by the way i tell you that i am using sqlserver 2005.

Thankss


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 2:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is the object really changed ? Maybe hibernate decides that no update is necessary ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: data changed
PostPosted: Tue Aug 26, 2008 2:26 am 
Newbie

Joined: Mon Aug 25, 2008 6:51 pm
Posts: 3
yes, the data is changed, i have tried creating data with just the name field with data, then in the update i set values for other fields.

If you can help me i can post here the web.config, class , hbm files, all is very simple, cause is the first table of the project what i am trying to update.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 2:36 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Strange. Can you try and skip the SaveOrUpdate call in the case you have an existing object.

And post the mapping file.

_________________
--Wolfgang


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