0

私は java+maven+spring+hibernate でプロジェクトに取り組んでおり、saveorupdate を呼び出す前に現在の日付を POJO に自動的に割り当てたいと考えていました。new Date()すべてのクラスのすべての date_created を作成してもかまいませんが、十分です。spring aop がそれらの点で優れていることを発見しました。
Googleで数分後、私はそれを行うための良い方法を見つけました.しかし、今までは、導入インターフェースの実装クラスであるPOJOに新しい日付を正確に割り当てる方法がわかりません.方法がわかりません意味をなすためにそれを置く.だから私の理解から、これは私が行う方法です:

//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**/
    }
}

ここで私が間違っていることを誰かに教えてもらえますか?または、これは私がやりたかったことを達成するための彼の間違った方法です.読んでくれてありがとう

4

1 に答える 1

1

Hibernate を使用している場合は、エンティティ リスナーを使用してプロパティを設定してからデータベースに永続化できます。

必要なのは、作成日を に設定する pre-persist リスナーだけですnew Date()

エンティティ リスナーに関する Hibernate のドキュメントは次のとおりです。

于 2010-09-13T16:53:14.067 に答える