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: Strange behaviour IsDirty
PostPosted: Wed Sep 05, 2007 9:46 pm 
Newbie

Joined: Wed Jul 18, 2007 3:54 pm
Posts: 2
How can I determine whether a session is Dirty or not without calling IsDirty, because this method performs a flush. My problem is, that I want to add objects to a list.

Code:
Word word = NHDataManager.LoadById<Word>(35, exchangeContext);
word.WordExpressions.Add(new WordExpression("some new expression", word));
NHDataManager.Save(word, exchangeContext);


The new WordExpression object is not ready for saving yet. In the Save method I want to set the missing values (created user, changed user) by use of reflection. Here I have to determine whether the object (graph) has been changed or not. When I call the IsDirty method, I got the following exception:

Util.Database.NHibernate.NHDataManagerException: could not insert: [Wisdom3000NH
Persistence.Entities.WordExpression][SQL: INSERT INTO [Word_Expression] ([change
d_at], [created_at], [synonym_code], [catalogue], [word_id], [changed_user_id],
[created_user_id]) VALUES (?, ?, ?, ?, ?, ?, ?)] in Util.Database.NHibernate.NHD
ataManager.SetTrackingValues ---> NHibernate.ADOException: could not insert: [Wi
sdom3000NHPersistence.Entities.WordExpression][SQL: INSERT INTO [Word_Expression
] ([changed_at], [created_at], [synonym_code], [catalogue], [word_id], [changed_
user_id], [created_user_id]) VALUES (?, ?, ?, ?, ?, ?, ?)] ---> System.Data.SqlC
lient.SqlException: Cannot insert the value NULL into column 'changed_user_id',
table 'Wisdom3000-Test.dbo.Word_Expression'; column does not allow nulls. INSERT
fails.

How can I avoid this annoying behaviour?

Of course, I could do something like this:

Code:
Word word = NHDataManager.LoadById<Word>(35, exchangeContext);
// Perform in this method the reflection stuff and then add the object to the list
NHDataManager.AddToList(new WordExpression("some new expression", word), word, exchangeContext);
NHDataManager.Save(word, exchangeContext);


This works fine, but it's pretty ugly. Does anyone have any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 2:06 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Try setting the session's FlushMode to FlushMode.Never while you do your stuff, then set it back to its original value when done:

Code:
NHibernate.FlushMode originalFlushMode = mySession.FlushMode;
mySession.FlushMode = NHibernate.FlushMode.Never;

try
{
    // do your stuff here
}
finally
{
    mySession.FlushMode = originalFlushMode;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 8:42 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Nels_P_Olsen wrote:
Try setting the session's FlushMode to FlushMode.Never while you do your stuff, then set it back to its original value when done:



Or you can use mySession.FlushMode = FlushMode.Commit so that flushes only happen during commits.


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.