-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using hibernate Interceptors
PostPosted: Mon Sep 28, 2009 4:29 am 
Newbie

Joined: Wed Sep 09, 2009 9:02 am
Posts: 3
I have used hibernate interceptors to populate the createdDate and modifiedDate property of an entity at insert and update operations respectively

heres my code implementation

Code:
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

public class SaveOrUpdateDateInterceptor extends EmptyInterceptor{
   
   public boolean onSave(Object entity, Serializable id, Object[] state,
         String[] propertyNames, Type[] types) {
      boolean b = super.onSave(entity, id, state, propertyNames, types);
      ((CommonPersistentProperties)entity).setCreatedDate(new Date());
      System.out.println(entity.getClass().getName());
      return b;   
   }
   
   public void preFlush(Iterator entities) {
      super.preFlush(entities);
      while(entities.hasNext()){
         CommonPersistentProperties entity = (CommonPersistentProperties)entities.next();
         entity.setModifiedDate(new Date());
      }
   }   
}


CommonPersistentProperties is an interface implemented by all DAO classes to implement getters and setters for createdDate and modifiedDate property

i'm new to hibernate, Is this a correct approach ?, Is there any side effects using this approach ?, are there any better approach to populate createdDate and modifiedDate property of an entity ?, please advice


Top
 Profile  
 
 Post subject: Re: Using hibernate Interceptors
PostPosted: Mon Sep 28, 2009 7:59 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
If you want to set a value of an attribute in an interceptor, you must use vectors but not entity. I mean, there are three vectors with names, types and values of every properties of an entity. If you want to set the value of a property you have to do it with that vectors.

First you have to obtain the index value of the property name (over propertyNames vector) and then, accessing with that index, you can set the value over state vector.

If you invoke setter method for the property to be set, it will be not established.

Also is important to return a true value in that interceptor's method to indicate hibernate that the state has been changed by your code.

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Using hibernate Interceptors
PostPosted: Mon Sep 28, 2009 9:56 am 
Newbie

Joined: Wed Sep 09, 2009 9:02 am
Posts: 3
I have integrated hibernate with my spring webapp

Well this is what i understand from hibernate java doc

onSave is called before an entity is saved or persisted to DB.
preFlush is called before entities are flushed to DB i.e flushed entities are either saved or updated.

createdDate property is only to be set once when the entity is made persistent (satisfied by onSave).
modifiedDate property is to be set when an entity is updated or saved (satisfied by preFlush).

Quote:
If you want to set a value of an attribute in an interceptor, you must use vectors but not entity. I mean, there are three vectors with names, types and values of every properties of an entity. If you want to set the value of a property you have to do it with that vectors.

First you have to obtain the index value of the property name (over propertyNames vector) and then, accessing with that index, you can set the value over state vector.


I want to populate createdDate, modifiedDate property with Date object for all the entities that are being saved or updated

Quote:
If you invoke setter method for the property to be set, it will be not established.


This approach worked for me, the columns are getting updated correctly. My concern is that is it a correct approach to the problem?
If not how to approach this problem correctly?

Quote:
Also is important to return a true value in that interceptor's method to indicate hibernate that the state has been changed by your code.


You are right, but i tried it earlier and it failed to update createdDate, modifiedDate property in DB. Still working on it.


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