-->
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: I need to execute DAO methods just before delete (PreDelete)
PostPosted: Fri Feb 20, 2009 10:47 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi all.

When I delete an object from database, I have to make other actions over the database. usually, I encapsulate these actions in a transactional delete method, where I call Hibernate API delete method.

I mean:

void deleteObject(Object o) {
openTransaction();
makeActions(); //these actions affect the database
delete(o);
closetransaction();
}

The problem is when an object o is deleted in cascade, so it doesn't invoke my deleteobject() method.

I want to embed makeActions() in a PreDeleteEvent.

Event is listened right, but when executing makeActions(), it doesn't work.
Can't I work with database in a Hibernate Eventlistener?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 12:37 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
there might be a better way to do this through hibernate's api.

But why not just make every object call the child's objects deleteObject. before you delete the object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 4:25 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
If I have to call the delete method for each child level, why exists a cascade mechanism?

Actually, the problem is a little bit more complex: I have specified CascadeOrphan, because I need to be able to remove an object from a collection (set) in a parent object, and to persist that deletion to DB.

Example:

class A {
Set<B> bSet;
//other properties and access (get/set) methods
}

class B {
//properties and access (get/set) methods
}

Executing the following sentences must remove b element from database. The only way I know to do that is with CascadeType.DELETE_ORPHAN annotation in A.setB property.

A.getBSet().remove(b);
saveOrUpdate(a);

In this scenario I can't to call delete method for b object, so I could "intercept" or "listen" the delete action in a preDelete event listener.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 4:44 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
It seems like you might be overcomplicating something. Without seeing why you need to call a deleteObject method before you delete an object it's hard to know.

If you delete an annotated pojo then the pojo is deleted. If you cascade then all the children are deleted. If there is something else you need to delete perhaps it should be attached to your annotated hierarchy as another child.

Pojo's actually are supposed to be just pojo's. They aren't supposed to contain any business logic.

Perhaps if you are trying to log something in the deleteObject or archive it, then you should wrap the 2 actions in a transaction and execute them simultaneously. This type of logic is usually done at a higher layer in the dao, in a manager or something similar.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 4:47 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I hate to do research for people but is this what your looking for

http://www.hibernate.org/hib_docs/refer ... vents.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:57 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
First of all, I want to thank you for the research. I'm not sure if what you have found is what I need. I had written an event before to publish the topic, but it did not work fine...

I'm going to clarify definitely the situation.

Code:
class A {
String idA;
Set<AB> bSet;
// other attributes and get/set methods
}

class AB {
String idA;
String idB;
// M-N relation attributes
}

class B {
String idB;
// other attributes and get/set methods
}


Like M-N relation has attributes, I need to write a class for it.
Notice that relation is unidirectional, I mean, A side looks B (via AB attribute) but B side can't see A side. This is not negotiable.

Now, what happens if I try to delete a B element? if I call delete method in B DAO, it would be responsible to update attribute A.setB, deleting AB elements.

The real problem comes when a B element in deleted in cascade by a parent element. So I need to intercept the deletion and defer A entities form B setting null values and deleting AB elements.

Is now the scenario complete? do you understand what I am oing and why I need to intercept the action?

Like I wrote at the beginning of the topic, DAO calls in a preDelete event does not work and I don't know why.

Any other comment or help about this?

Best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 10:48 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
The interceptor seems to be what you want. You might want to post a new topic so somebody can help you with the problem you are having with that. I have never used them so, I could only guess at it..

I still think that you can simplify this simply by adding the logic to your manager layer. There is no reason why you could not delete B and then go back and remove all AB's where b doesn't exist. Or just delete AB where it has the fk of the previously delted b.

I can't say I am an expert on hibernate, but i don't believe that what your doing will be executed atomically anyway. Unless of course you put it all in a transaction which you could do in your manager layer anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 8:50 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Finally I have reached my mark!!!

Now I'm using an interceptor http://www.hibernate.org/hib_docs/reference/en/html/events.html and it works just like I was looking for. This is: just before make effective any object deletion, I can intercept the execution flow and run sentences doing what I need.

The only extra considretaion is, like those actions access to the DB, i need a hibernate session/transaction and it MUST be a new session, I mean, I have to use REQUIRES_NEW instead REQUIRED propagation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 9:08 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Glad you found a solution to your problem.

_________________
Please rate my replies as I need points for all of my questions also.


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.