-->
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: How to intercept associations
PostPosted: Thu Jun 29, 2006 9:21 am 
Newbie

Joined: Mon Nov 08, 2004 4:05 am
Posts: 6
Hibernate version: 3.1

Friends,

I am trying to create an Audit interceptor to track all entity changes in the application. This also includes tracking all new associations.

I am having problems with intercepting unidirectional one-to-many association. I tried this with some simple entities to work on the problem. Here are the mapping files :

Mapping documents:

Class : Client

Code:
   <class name="com.test.Client" table="CLIENT" lazy="false">
      <id name="clientNumber" type="integer" unsaved-value="null" >
         <column name="clientNumber" not-null="true"/>
         <generator class="increment"/>
      </id>
      
      <property column="title" name="title" type="string" />
      <property column="lastName" name="lastName" type="string" not-null="true"/>
      <property column="firstName" name="firstName" type="string" not-null="true"/>

      <set name="references" inverse="false" cascade="delete">
         <key column="client_id" />
         <one-to-many class="Reference"/>
      </set>
   </class>




Class : Reference

Code:
   <class name="com.test.Reference" table="Reference" >
      <id name="id" type="integer" unsaved-value="null" >
         <column name="id" not-null="true"/>
         <generator class="increment"/>
      </id>
      
      <property column="name" name="name" type="string" not-null="true"/>
      
   </class>



Here is my test case :
Code:
   public void testUnidirectionalAssociation() {
      Client client = new Client();
      client.setClientNumber(null);
      client.setFirstName("Steve");
      client.setLastName("Jobs");
      client.setTitle("Mr");
      
      dao.persist(client); // Client object is saved and flushed

      Reference r = new Reference();
      r.setName("Client type");
      dao.persist(r); // Reference object is saved and flushed

      client.addReference(r);
      dao.persist(client);
   }


Now, my AuditInterceptor's onSave() method gets called when the Client and Reference objects are created. But when there is just an association change, the onFlushDirty() methods do not get called.

We do have an onCollectionUpdate() callback which however sends across the entire collection. How do we find the newly added (or the "dirty") entities ?

Any help on how to track such association changes please ?

Thanks so much.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 1:50 am 
Newbie

Joined: Mon Sep 26, 2005 8:43 am
Posts: 16
Can anyone reply to this post? I am also facing similar problem and do not know way out. I am using Hibernate 3.0.5 where in Interceptor doesn't have onCollectionUpdate API itself. So I decided to switch on to Hibernate 3.2.4.sp1 wherein onCollectionUpdate API of Interceptor provides with all the objects of a particular collection rather than the changed ones.

So I have a Student table and Student_Addresses table. One Student can have multiple addresses. This association is managed using idbag in the mapping file. I want to audit last when was the student address edited. So I have implemented the Interceptor to intercept calls to have latest timestamp on lastmodified column of Student_Addresses table before commit to database. But onCollectionUpdate API provides me with all the address of a particular student and not the changed ones.

Is there any other appropriate API for this purpose?

_________________
--Beejal


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.