views:

30

answers:

1

Hello Gurus !!!

i've been working on a project in java+maven+spring+hibernate and i wanted to automatically assign the current date to the POJOs before the calling of saveorupdate. i wouldn't mind creating new Date() for all the date_created of all the classes but they are just plenty. I've just discovered that spring aop is good at those things.
After few minutes on google i found a nice way to do it.but up to now i can't see how exactly i can assign new date to POJO which are then implementor classes of my introduction interface.i don't really know how to put it to make sense.so from my understanding this is how i would do:

//DateSetter.java
package org.personal.myproject.model;

import java.util.Date;
//this is how i'm getting the current date
public interface DateSetter {

   public Date getCurrentDate();

}


//DateCreatedDateSetterImpl.java
package org.personal.myproject.model;

import java.util.Date;
// the implementation
public class DateCreatedDateSetterImpl implements DateSetter {

  public Date getCurrentDate() {
    return new Date();
  }

}

//DateIntroduction.java
package org.personal.myproject.model;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;

@Aspect
public class DateIntroduction {
    //making all the model implementors of the DateSetter interface
    @DeclareParents(value="org.personal.myproject.model.*",
    defaultImpl=DateCreatedDateSetterImpll.class)
    public DateSetter dateSetter;

    /**here is to add my "before trigger" for every calling of every method.
       if you can see i have the daos in 2 packages, i don't know whether is wrong or         not.i'm actually using the generic Dao so should i use that interface instead?
    **/
    @Before("execution * org.personal.myproject.dao.hibernate.*.(..) || *    org.personal.myproject.dao.hibernate.*.*.(..)" + "&& this(dateSetter)")
    public void injectLastModifiedDate(DateSetter dateSetter){
      /**so here i'm expecting to inject the new date but apparently i'm missing something**/
    }
}

Can anyone show me what i'm doing wrong here?Or this is just he wrong way to achieve what i wanted to do.Thanks for reading

+1  A: 

If you are using Hibernate, you can use entity listeners to set the property before persisting it in the database.

All you need is a pre-persist listener that sets the creation date to new Date().

Here is the Hibernate documentation on entity listeners.

Vivien Barousse
thanks reading now!!but what if it was last modified that cannot be set in the database?
black sensei
ok with the method i'll have to do the same for all the entity then.not really usefull when everything is already created and their number significant.thanks anyway
black sensei
For the last modification date, the concept is the same. You just have to specify a pre-update listener instead of a pre-persist listener.
Vivien Barousse
Thanks for your help.So in case of 30 POJOs for example i'll have to do that for each of them?
black sensei
@black sensei: no, make them all extend a base class like BaseEntity and implement the functionality for that class.
seanizer
Yes, that's something you need to do on an Entity level. I don't think you can do this for all your entities. But if your listener is generic enough, you just have to declare the listener of your 30 classes, not write a specific listener for each.
Vivien Barousse
While I like to throw AOP at all kinds of problems, this one is definitely solved better using hibernate listeners (+1)
seanizer
@sensei: maybe you should use jpa, @prepersist and @preupdate work nicely with object hierarchies.
seanizer