-->
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.  [ 1 post ] 
Author Message
 Post subject: Modifying persistent object references within a listener
PostPosted: Tue Nov 21, 2006 11:03 pm 
Newbie

Joined: Tue Mar 14, 2006 5:06 pm
Posts: 4
Hibernate version: 3.1.3
Database: PostgreSQL 8.1.4

Hello,

Our object model is similar to the composite pattern (Class Xyz contains references to other Xyz), and we would like to configure a save/insert listener so that the listener runs for each instance in an object graph. (My main question is below, however).

Domain model Example:


Code:
public class Xyz {

  private Long id;
  private Xyz specialMember;
  private Collection<Xyz> memberCollection;
  private User primaryAssignee;
  private Collection<User> assignedUsers;
  // ...etc.
}


In the example above when an Xyz instance is saved, e.g.
Code:
session.save(myXyz);
I would like the listener to run once on the myXyz instance, and once on each contained instance of Xyz.

My (limited) understanding is that I could use the FlushEntityEventListener or possibly the PreInsertEventListener for certain kinds of changes.

The complication is that we also hope for this listener to modify one or more object references of an Xyz instance. In the example below, I use a temporary session to retrieve a different object that we hope to associate.

Listener example:

Code:
public void onFlushEntity(FlushEntityEvent event) throws HibernateException {

  String[] propertyNames = event. getEntityEntry().getPersister().getPropertyNames();
  Object[] currentState = event.getPropertyValues();
  Session currentSession = event.getSession();
  Session tempSession =   currentSession.getFactory().openSession(currentSession.connection());

  //Retrieving arbitrary user for example
  User newAssignee = tempSession.get(User.class, new Long(27));
       
  for ( int i=0; i<propertyNames.length; i++ ) {
            if ("primaryAssignee".equals(propertyNames[i]) ) {
                currentState[i] = newAssignee;
            } else if ("assignedUsers".equals(propertyNames[i]) ) {
                Collection assignees = currentState[i];
                assignees.add(newAssignee);
            }
  }
}


My main question is whether it is possible to make a change within a listener (e.g. to a collection or another persistent object reference) that will be picked up by hibernate, if the current save operation has already been cascaded, as it would have to be in order for this listener to run on all instances of class Xyz.

My secondary question is whether it would be necessary (and possible) to re-attach a managed object to the main session within the listener; i.e. would I have to re-attach the newAssignee in the example above before associating it with the currentState of the persistent object? Is this allowed?

I think that related to this might be that I am somewhat unclear on the differences in the EventListener contract between FlushEntityEvent.entity and FlushEntityEvent.propertyValues.

Thanks for any help or perhaps recommendations on other approaches to modifying persistent object references within a listener or an interceptor.

Kind regards. --Dan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.