-->
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.  [ 7 posts ] 
Author Message
 Post subject: How can I perform complex post-flush operations?
PostPosted: Wed Oct 15, 2003 8:09 pm 
Newbie

Joined: Wed Oct 15, 2003 7:49 pm
Posts: 3
I am using Hibernate in a complex system. Often I need to perform further work on entities after they are created or saved.

Here is an example: There is a User entity, and an Audience entity, and a UserAudienceAssociation entity that is the relationship between the two. When a User is created, it is necessary to create UserAudienceAssociations. Similarly, changing or deleting a User may make it necessary to create and/or delete UserAudienceAssociations.

But: It is not permitted to call Session methods from callbacks. I assume this means that I cannot write postFlush() or onSave() methods that save() or delete() UserAudienceAssociations? Is this true? And if it is true, what can I do to keep my objects up to date?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 8:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
session.flush()


Top
 Profile  
 
 Post subject: Re: How can I perform complex post-flush operations?
PostPosted: Wed Oct 15, 2003 8:24 pm 
Newbie

Joined: Wed Oct 15, 2003 7:49 pm
Posts: 3
I'd appreciate a more detailed answer. I'm not a newbie. I'm trying to do some very complex things using Hibernate. I know all about flush().

I do call flush() after save(), if that is what you mean that I should do. My problem is that I can't save() or delete() related objects from SessionInterceptor or Lifecycle methods. So there is no way for me to automatically fix up the state of an object after its changes are persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can save and delete from Lifecycle callbacks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
(but why would you not just use cascades?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 8:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I don't know enough about you complex manipulation though in general, the type of operations you are talking about, eg, maintaining associations between the domain objects, would normally be performed as a part of the Transaction Script. Just use the domain objects and if necessary cascade the operations.

void doNewUser() {
User u = new User(); // Set all the values etc.
Set uAudience = new HAshSet();
uAudience.add(new Audience()); // Set up all the Relationship
u.addAudiences(uAudience);
sess.save();
sess.flush(); // Flush at the end of the logical set of operations.
}

You may be using the hibernate this way. Not knowing more detail your business processes should be able to be captured as above (more complexity may require a large DAO hierarchy).


Top
 Profile  
 
 Post subject: Re: How can I perform complex post-flush operations?
PostPosted: Wed Oct 15, 2003 9:05 pm 
Newbie

Joined: Wed Oct 15, 2003 7:49 pm
Posts: 3
Gavin wrote:

>You can save and delete from Lifecycle callbacks.

Yes, but the Lifecycle callbacks are called before the operation is performed. This is a particular problem for onSave(), because at that point the master object doesn't have an identifier. This limits my ability to create associations.

>(but why would you not just use cascades?

Maybe cascades would help me with some relationships. (Can you point me to an explanation? I grepped Cascade out of the JavaDoc but could not find any place where it was defined. I can see the Cascade JavaDoc, but not an explanation of what to do with it.) But some activities that I would like to do in postFlush() can't be accomplished with cascades.

For instance: I have entities, called Domains, which have a parent-child relationship that is defined by a mapping table. When that mapping table is updated, triggers cause objects to be created and deleted in another mapping table. (Yes, this is horrible, but the database was designed long before we were using Hibernate.) So when Domain relationships change, I would like to do some code in postFlush() to load new entities to represent the changes that were performed by the database triggers.

By the way, I really appreciate you and David's prompt responses. I am trying to do some pretty involved stuff using Hibernate. The purpose of this post is to see if anyone else had this experience and had a solution that I could learn from.


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