-->
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.  [ 8 posts ] 
Author Message
 Post subject: DELETE and UPDATE...
PostPosted: Thu Jun 09, 2005 11:53 pm 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Hi, I'm not sure if I'm doing my UPDATE's and DELETE's the proper way:

Basically, I want to make sure what I updates, or deletes, in fact exists (ie not deleted by another user) before I actually perform the operation:
Code:
o_session = SetupBISConnection(...)
trx = o_session.BeginTransaction()

Try
  'IF the object with specified UIN does not exist (that it's deleted by another user), then NHibernate will throw ObjectNotFoundException.
  obj = o_session.Load(GetType(CObj), UIN)
Catch ex As NHibernate.ObjectNotFoundException
  'object to be updated does not exist - abort delete
  Throw ex
End Try

'Okay, if no exception so far, it means the object is there...
o_session.Delete(plan)
o_session.Flush()

trx.Commit()

Any pointer? Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:26 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should use Get, not Load for this, because if the object has proxies Load will return a proxy without actually visiting the database to check whether the object exists. But otherwise it should work fine.


Top
 Profile  
 
 Post subject: Load --> Then Update
PostPosted: Sat Jun 11, 2005 9:20 pm 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Hey Thanks a bunch sergey.

QUESTION 1:
Yes, I tried "Load" then "Update" it gave me weird error complaining another instance with same identifier already exists...? Why is it saying that? I'm trying to UPDATE of course "another" instance would have existed..?

QUESTION 2:
Also, why "NHibernate.ObjectNotFoundException" NOT thrown when the instance I'm trying to update has been deleted by another user? I thought it'd be more intuitive than a generic Hibernate exception.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 12, 2005 4:19 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, you don't have to call Update if your object was loaded in the same session, the session already knows about it. As for why that exception is not thrown, I don't know, it was probably this way in H2.0.3 and nobody changed it yet.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 12, 2005 9:12 pm 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
So, just:

1. Load(GetType(MyType), UIN)
2. Make changes:
obj.SomeProp = x;
3. o_session.Flush();

That's it? Is this the proper way to do UPDATE? I'd assume you need to wrap Flush inside a transaction AND a try-block in case if object corresponding to the selected UIN has been deleted. Is this ...?

try {
trx = o_session.BeginTransaction();
obj = o_session.Load(GetType(MyType), UIN);
obj.Prop1 = "xxx";
o_session.Flush();
trx.Commit();

} catch(ObjectNotFoundException e) {
//1. alert user object to be updated has been deleted.

//2. in case if you've done other stuff in transaction...
trx.RollBack()l
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 12, 2005 9:46 pm 
The code you have proposed has a couple of subtle issues. The first issue is that your transaction will only rollback if an ObjectNotFoundException is thrown. However, other exceptions may be thrown as part of executing the try block and if so, your transaction won't rollback until the garbage collector comes and gets it.

The second issue is that you are using exceptions to as part of your regular program execution path. Exceptions in .NET are slow and as such should only be used to handle exceptions rather than control program flow.

I would suggest replacing the try/catch block with a "using" block:

using (trx = o_session.BeginTransaction())
{
...
trx.Commit()
}

Also, I would suggest using the Find method instead of the Get method to load your object if you feel there is a chance that the object may not exist as part of the normal program flow. Then check the Count property on the resultant IList implementation returned. If Count is zero, then alert the user the object has been deleted. Otherwise, set your object to the first item in the list.


Top
  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 11:15 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Or just use Get, not Load. Get will return null if the object was not found in the database. Also, transaction.Commit will call Flush, so you don't have to call it yourself. But otherwise the code you wrote should work fine.


Top
 Profile  
 
 Post subject: Thanks guys.
PostPosted: Tue Jun 14, 2005 3:24 am 
Regular
Regular

Joined: Mon May 30, 2005 11:20 pm
Posts: 66
Thanks guys, that helps.


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