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.  [ 4 posts ] 
Author Message
 Post subject: Why is hibernate committing dirty data when I do a SELECT
PostPosted: Sat Sep 06, 2008 5:18 pm 
Newbie

Joined: Sat Sep 06, 2008 4:55 pm
Posts: 5
My issue is, I have a bunch of code that seems to be just a bunch of select statements, and yet for some reason, hibernate is committing dirty data. I don't want it to commit the new article to the database yet (maybe not until the next postback...I am using ASP)




Hibernate version:1.2

Code between sessionFactory.openSession() and session.close():

session = factory.OpenSession();

Magazine magazine = session.Get(typeof(Magazine ), 1);
magazine.Articles.Add(new Article("testText"));

IQuery query = m_session.CreateSQLQuery("SELECT Count(*) as ItemCount FROM PublisherEmployees WHERE FK_PublisherId=" + magazine.Publisher.Id).AddScalar("ItemCount", NHibernateUtil.Int32);
int numArticles = (int)query.UniqueResult();

session.Flush();
session.Close();



The only time I every want to commit data is when I call the save function below. Which I do not call in the above code

void Save(object item, bool pointlessParameter)
{
ITransaction tx=null;

try
{
tx = m_session.BeginTransaction();
m_session.SaveOrUpdate(item);
tx.Commit();
}
catch (Exception ex)
{
if (tx!=null) tx.Rollback();
throw ex;
}
}

Full stack trace of any exception that occurs: No errors

Name and version of the database you are using: SQL Server 2005 Express


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 2:13 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
So why do you call session.Flush() if you don't want to change data? ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 10:28 am 
Newbie

Joined: Sat Sep 06, 2008 4:55 pm
Posts: 5
You can probably tell I am no nhibernate expert (yet). I thought flush just cleared the persistant data so that it would get fresh data from the server. I did not realize that flush commits the data. Are you saying that flush commits all dirty data to the database?

One thing too, I notice that it seems to commit dirty data one this line

IQuery query = m_session.CreateSQLQuery("SELECT Count(*) as ItemCount FROM PublisherEmployees WHERE FK_PublisherId=" + magazine.Publisher.Id).AddScalar("ItemCount", NHibernateUtil.Int32);

any ideas why that would be the case?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 1:12 pm 
Newbie

Joined: Sat Sep 06, 2008 4:55 pm
Posts: 5
ok, looks like I need to call clear() to get rid of the persistant data and flush to synchronize. By setting the flush mode, I should be able to control when it does it. Thanks


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