-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to know if objects are deleted?
PostPosted: Thu Apr 27, 2006 8:42 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
NHibernate version:1.02

Hi all!
How to know if objects are deleted?
This might seem as an "easy question", but I really could not find anything in the docs... or in the faq... I was looking for something like session.IsDeleted(object) or session.IsTransient(object). But I couldn't find anything...
I need to know not only if session.Delete(object) was called on an object... but also if it was actually deleted (maybe the transaction was cancelled)
thanks!
bye


Last edited by luxspes on Thu Apr 27, 2006 9:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 8:53 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Not a graceful solution but you could just try retrieving the record again and if you can't get it then it's probably deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 9:41 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Quote:
Not a graceful solution but you could just try retrieving the record again and if you can't get it then it's probably deleted.


Thanks for you answer... and yes... I have been thinking about doing precisely that... but I still wonder if ther isnt a better way of doing it without making a trip to the database...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 3:57 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi.

I think you should control this in your mapping classes, using a property called, for example, "IsDeleted".

When you have a list of objects and delete some of them, call obj.MarkAsDeleted(), for example.

I've got this using the MyGeneration. When you generate your mapping classes/xmls, the MyGeneration Software adds two properties to control if the object is changed or deleted.

Looks like this:

Code:
// ...
private bool _isChanged;
private bool _isDeleted;

// ...

/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsChanged
{
    get { return _isChanged; }
}


/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsDeleted
{
    get { return _isDeleted; }
}

// ...

/// <summary>
/// mark the item as deleted
/// </summary>
public void MarkAsDeleted()
{
    _isDeleted = true;
    _isChanged = true;
}


The "IsChanged" property changes its values when you call the method Set from any property from your object.

Take a look at the definition of a property:

Code:
public int? Sexintervalo
{
    get { return _sexintervalo; }
    set { _isChanged |= (_sexintervalo != value); _sexintervalo = value; }
}


As you can see, it's easy to get the state of an object.


I've never used this approach ... but I think this will help me when I start to use colletions (master detail, for example).

Bye!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 4:09 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!
Yes, but... what would you recommend to do if the transaction fails while deleting?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 4:21 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
luxspes wrote:
Hi!
Yes, but... what would you recommend to do if the transaction fails while deleting?


If the transaction fails while you're deleting an object, you just don't call MarkAsDeleted().

I think you are trying to use a master/detail and control the state of the items (details), right?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 29, 2006 8:55 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Mmmmm you are right, I should only mark them after the transaction ended succesfully.
The problem I am having is that I search for an object in a window,then edit one of the objects in the list in another window, where I may choose to delete it, and after that the "edit" window dissapears and I am presented again with the "search results list" window... that shows an object that I already deleted, so I need to know that I deleted it to remove it from the screen


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 12:48 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
luxspes wrote:
Mmmmm you are right, I should only mark them after the transaction ended succesfully.
The problem I am having is that I search for an object in a window,then edit one of the objects in the list in another window, where I may choose to delete it, and after that the "edit" window dissapears and I am presented again with the "search results list" window... that shows an object that I already deleted, so I need to know that I deleted it to remove it from the screen


Ok... after delete the object, you just mark it as Deleted (call the MarkAsDeleted() method) and when the edit window closes, you refresh the "search result list" or just load the objects again (I thinks this is the better way).

Bye!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 3:35 pm 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
mark your object as deleted 'by user'. Then do a session.delete and flush. Use the interceptor from nh to give you a sign and just mark the objects as 'really deleted' or actually delete them (since this time, they should really be evicted from memory).


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