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: Commit sending updates for every record in the collection
PostPosted: Thu Aug 14, 2008 6:32 am 
Newbie

Joined: Mon Aug 11, 2008 5:45 am
Posts: 15
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

1.2:

Transaction.commit():

I have a sql server table with a Datetime Column with Allow NULL. I am loading all the records from that table via NHibernate then I iterate through the collection take out my required Record(Object) from that collection, make some changes to it ( I assign value to DateTime field) and then I save and commit this object. All this happens in a single Session.

My problem is that when I commit the transaction I get an exception saying that DateTime should be between 01/01/1700 to 31/12/9999.

When I enabled the logging of SQL queries I found that when I call transaction.commit() it sends updates for all the records in the collection whereas I expected it to only send the updates for the record which I changed. Anyways, as it is sending update for every record in the collection some of them have NULL datetime field and due to which the whole transaction fails.

Following is the algorithm I am using.

Session = CreateSession();
Transaction = Session.BeginTransaction();

Queue = GetQueue(QueueId);
Message m;
foreach( Message m in Queue)
{
If ( Message.status.equals("NEW") )
{
m = Message;
break;
}
}

m.processingTime = DateTime.NOW;

Session.Save(m);
Transaction.Commit();
Session.Close();


I know that whenever we commit a transaction it synchronizes with the database but I expected only changed records to be updated not everything.

Lastly I know that it is not the correct way to do what I have done. I should have a specific HQL query fetching the record directly from database. But I am testing different things so it would help me understand the behaviour.

Help is much appreciated

Thanks
Syed


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 7:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You don't need the save when the objects are still in the session. If you make an additional Save on a object that is already in the session, normally nothing should happen. But I think in that case it cascades the explicit save to the items and executes the corresponding updates.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Found the Answer
PostPosted: Thu Aug 14, 2008 9:50 am 
Newbie

Joined: Mon Aug 11, 2008 5:45 am
Posts: 15
I have found the issue. It has nothing to do with save but the problem lies in the fact that C# doesn't allow a NULL DateTime field where as SQL Server allows it.

When we loaded the Collection or Queue object it loaded all the messages in that queue. Some of those messages had empty DateTime column so C# had put default value 01/01/0001 in that field. When I comitted the transaction NHibernate saw that DateTime field had changed for every Message Object and it issued an Update.

See the article below to resolve this issue.

http://www.ayende.com/Blog/archive/2007 ... ssues.aspx

Anyways thank you very much for posting me a reply.

Cheers,
Syed


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.