-->
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.  [ 5 posts ] 
Author Message
 Post subject: Why does NHibernate update a wrong object?
PostPosted: Tue Jul 15, 2008 6:44 am 
Newbie

Joined: Tue Dec 19, 2006 4:59 am
Posts: 8
Hi all

I have a problem in the following situation:
I have two persistant classes vwTransaction and Transaction. The first one is populated from the view vwTransactions, the second - from table Transactions.
To populate the view data on the screen I'm using vwTransaction, and on the update statement, I'm using Transaction.

public void FillObject(int tranID)
{

ITransaction tx = FactorySession.BeginTransaction();
VwTransaction lVwTransaction = FactorySession.Load(typeof(VwTransaction), tranID) as VwTransaction;
FactorySession.Flush();
tx.Commit();
//....init UI controls with data from lVwTransaction
}

public void Save()
{
Transaction tran = FactorySession.Load(typeof(Transaction), CurrentEntity.ID);
//....source to change the fields of tran object
tx=FactorySession.BeginTransaction();
FactorySession.SaveOrUpdate(tran);
tx.Commit();
}

FactorySession - ISession, stored in the Session of the ASP user.

When the tx.Commit is run the error occurs: "can't update object vwTransaction [SQL Statement: UPDATE vwTransaction SET ....]". But I don't want to update this object - I want to update Transaction!!! Why this happens?

In the ASP Page the FillObject is in the method Page_Load occurs on any postback event. I think I have to clean up somehow the select statements againts the previous object before to run the update. But what I have to do? Close the session?

Thanks in advance for your help

Hibernate version: 1.2.0


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 6:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Your description sounds like you change the vwTransaction after loading and the write the changes to this object into the transaction object ? In that case, I assume, the vwTransaction instance is in the same session as the Transaction instance this hibernates tries to persists both. There are a few possible solutions:

1) clear session after loading/before saving
2) evict the vwTransaction instance after loading
3) add mutable="false" to the mapping of vwTransaction
<class name="vwTransaction" mutable="false" .... >

Btw. you don't need an explicit SaveOrUpdate() call if the object is already in the session. The object will be updated automatically when the session is flushed (which happens if you commit the transaction). And you shouldn't flush the session inside a BeginTransactio()...Commit() block.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 7:26 am 
Newbie

Joined: Tue Dec 19, 2006 4:59 am
Posts: 8
Hi, wolli.
Thanks for your quick reply. I don't try to change vwTransaction object at all, but I think you're right about instance of vwTransaction and Transaction in the same Session, because the session is stored in the Sigletone of IIS process. I don't close the session after the select activities.

1. Sorry for stupid question, but how can I clean the Session? Is it not enough just to Commit and Flush?
2. What does it mean "evict"?
3. I'll try to do it :)

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 7:59 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
1) session.Clear(). Neither Commit nor Flush clear the session. They "only" persist the changes you made to the objects in the session

2) session.Evict(obj): removes an persisted object from the session

But there must be some changes to your vwTransaction instances, otherwise hibernate wouldn't try to update the database.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 9:29 am 
Newbie

Joined: Tue Dec 19, 2006 4:59 am
Posts: 8
Thanks for your help. Clear() works for me :)


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