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.  [ 2 posts ] 
Author Message
 Post subject: PostUpdateEvent: get the Parent Object Unidi
PostPosted: Thu Jun 28, 2007 5:59 am 
Newbie

Joined: Thu Jun 28, 2007 5:41 am
Posts: 1
Hi,

We are implementing auditing/logging of object changes with the hibernate3 Event architecture. At the moment we have a problem with unidirectional one-to-many associations.

Here is a simplified example of the problem:

I have a unidirectional one-to-many association with the following mapping:

Code:
<hibernate-mapping >
   <class name="Parent" table="parent">
      <id name="id" column="id_parent" type="long"/>   
                <set name="children" lazy="false">
                   <key column="id_parent" foreign-key="fk_child_parent"/>
                   <one-to-many class="Child"/>
                </set>
   </class>
</hibernate-mapping>


<hibernate-mapping >
   <class name="Child" table="child">
      <id name="id" column="id_simplechild" type="long"/>   
      <property name="name" column="x_name"  type="string" />
   </class>
</hibernate-mapping>



We wrote a PostInsertEventListener to log the changes on the objects. While handling the event for Child objects we have this problem:

Is there a way to find the parent Object for this Child object. The problem is, that there is no navigation from the Child to the Parent Object and there also is no property in the hibernate mapping for this navigation. I searched the api docu for the PostInsertEvent and the related classes but found nothing that helped me further.

I know that a possible solution would be to design the association bidrectional. But unfortunately I can't change the design of the associations.

We work with hibernate 3.2.3

Thanks for any help you can give me.
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 3:37 pm 
Newbie

Joined: Tue Dec 04, 2007 7:50 pm
Posts: 4
Hi, I have something that may help you. In my case is after an object is updated (onPostUpdate)

Code:
public synchronized void onPostUpdate(PostUpdateEvent event) {
         Log.log.debug(event.getEntity());
         Class toSaveClass = event.getEntity().getClass();
         Log.log.debug("Object updated: " + toSaveClass + " ID: " + event.getId());
         try {
            updateEvents.put(new SubscriptionEvent(this, toSaveClass, event.getId()));
            fireUpdateToParents(event.getEntity(), event.getEntity().getClass());
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
   }

private synchronized void fireUpdateToParents(Object object, Class persistedClass) {
      ClassMetadata catMeta = HibernateUtil.getClassMetadata(persistedClass);
      String[] propertyNames = catMeta.getPropertyNames();
      Type[] propertyTypes = catMeta.getPropertyTypes();
      for ( int i=0; i<propertyNames.length; i++ ) {
         Class classType = propertyTypes[i].getClass();
         if ( (classType.equals(ManyToOneType.class) || classType.equals(OneToOneType.class)) &&  propertyTypes[i].isEntityType()) {
            Object value = catMeta.getPropertyValue(object, propertyNames[i],  EntityMode.POJO);
            if (value != null) {
               try {
                  Class parentClass = propertyTypes[i].getReturnedClass();
                  Object idValue = Persistence.getIDValue(parentClass, value);
                  Log.log.debug("Fired update to parent: " + parentClass + " ID: " + idValue);
                  updateEvents.put(new SubscriptionEvent(this, parentClass, idValue));
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      }
   }



PD: The ClassMetaData can be reached via: sessionFactory.getClassMetadata(class);


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