-->
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: onPostDelete and temporary sessions
PostPosted: Thu Oct 20, 2005 5:57 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Hibernate version:3.1

I'm trying to figure out how to use the event handlers in Hibernate. Here is what I'm trying to accomplish:

I have a Parent object and a Child object. I have a post-delete event handler for the Child object. When a Child is deleted I want to check if there are any other siblings under the Parent. If not, I want to delete the Parent.

From what I have been able to find here and there, it sounds like event handlers may not use the session that the object causing the event is attached to. We have to use a separate Session, and the recommendation is to use a 'temporary' session created from the same connection.

This is what I am doing, but it is causing some headaches. When I call delete(Child), the event handler is called, and I try to delete the Parent, because it has no other children. This fails because the Parent delete gets to the database before the Child delete (I have to flush the temporary session and close it inside my event handler code).

First, is this expected behavior? Second, is there any way to work around this? Since I'm handling a post-delete event, I was hoping that the handler could behave as though the delete already happened. I need to queue up my Parent delete so that it happens AFTER the Child delete. Is there any way?

Does documentation exist somewhere on the event system yet? I haven't been able to find much information, and the javadoc is mostly empty. Anyway, thanks for any help you can provide.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 3:46 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I'm still struggling with event handlers. My current roadblock is with the post-insert event and identity columns. It's similar to the post-delete problem from my previous post.

When an object is inserted, I want to insert a history record into a history table for that object, in the event handling code. There is no foreign-key relationship because it's a history table, so the history object has an Id field instead of an association.

When the event is fired, the object still has not been given an Id, even though it's a post-insert event. So the Id field in the history record is being set to null, and that fails because of a not-null constraint on the Id column. This does work fine if the database is using sequences, but I need it to also work with identity columns.

I guess my problem is that I fail to see the use of having post-X events if we can't assume that X has happened! Am I missing the point of the event system? Am I using it in a way it was not meant to be used? Has anyone else been able to get this type of thing to work?

Thanks in advance.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 7:50 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Ok, I just stumbled onto post-commit-insert and post-commit-delete events that are in the DTD in the latest 3.1 jar. They seem to be exactly what I'm looking for, from what I can tell so far.

_________________
nathan


Top
 Profile  
 
 Post subject: Barely the same problem.
PostPosted: Fri Nov 04, 2005 11:35 am 
Newbie

Joined: Fri Nov 04, 2005 11:12 am
Posts: 1
I am building an History system in an application, and I'm facing the same problem. Here is my concept. For each object that is marked to be "Historizable", we must always copy the integrality of the columns in another database, in a schema that is almost the same as the original(plus some columns saying the type of manipulation, the date etc). In my history version of the database, the ids of the objects aren't primary keys, but are logged. Foreign keys are also copied since we need to be able to query this database almost as if it were the current database.
For doing this, my strategy was to capture, in example, my insert with postInsert event, then reconstruct a select string that will select all the columns of the newly inserted object, and, based on the result, construct and execute an insert String with the additionnal information.

The idea isn't perfect, but it would have worked if the postInsert did really trigger after the data has been inserted in the database.

I also tried with interceptors, but I'm facing another problem : All the object aren't in the database when postFlush is called. In exaqmple, I have a parent object and a child object (a set on childObject in the parent object). All of these aren't already in the database so this is just all inserts. PostFlush is called an I loop through the Iterator of the "flushed" objects, and then making selects and building inserts for each ones. The most bizar thing is that it work for my child objects(the are read from the database then inserted in the other), but not for my parent Object : At the time of the postFlush, the select string is constructed, executed, but return nothing, even if at the end of the entire process the data as been correctly inserted in the current database. Anyone could help with suggestions on this ? The solution to my problem seems to be in the Hibernate 3.1 postCommitXXX, but since it is in beta version, I cannot allow myself to mount it in production. Do anyone know when the 3.1 version will be released ?


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.