-->
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: Entities Update Without Calling Update(...)
PostPosted: Fri May 09, 2008 11:33 am 
Newbie

Joined: Mon Aug 06, 2007 12:57 pm
Posts: 4
Probably a basic question, but I'm a little confused here. Why are UPDATEs generated when I never specifically call Update?

Code:
ISession session = NHibernateSessionManager.Instance.GetSession();
session.BeginTransaction();
PurchaseOrder po = new PurchaseOrder();
session.Save(po);
po.Description = "test";
session.Transaction.Commit();

or
Code:
ISession session = NHibernateSessionManager.Instance.GetSession();
PurchaseOrder po = new PurchaseOrder();
session.Save(po);
po.Description = "test";
session.Flush();


I realize the po entity is dirty when setting the description property and that is why it being persisted but is there anyway to stop the session from persisting an entity unless specifically calling session.Update(po)?


Top
 Profile  
 
 Post subject: Entities Update Without Calling Update(...)
PostPosted: Sun May 11, 2008 6:07 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

NHibernate uses an Unit of Work (http://martinfowler.com/eaaCatalog/unitOfWork.html) to track all changes made to persistent objects. It is correct behaviour for NHibernate it to persist changes to entities automatically.

Session.Update() takes a transient entity, and updates the persistent entity with the same identifier. It does not (necessarily) write an update to the database.

Session.Flush() is what writes any pending changes in the Unit of Work to the database.

The default flush-mode is 'auto', so NHibernate will write changes automatically before making certain queries on the database, or when you commit your transaction. The flush-mode can be set to 'never' if you do not want NHibernate to update the database and you wish to do this manually.

If you have a persistent object you want to make changes to, without the changes being persisted, you can Evict() the persistent entity from the session.

Hope that helps to clarify how NHibernate works.

Regards,
Richard


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.