2017-02-16 68 views
0

我使用Spring Boot,运行于v1.5.1.RELEASE,Spring v4.3.6.RELEASE。我试图对我的JPA事件监听器有点聪明,但它不工作;JPA EventListeners不工作

我的实体看起来像这样

@Entity 
@EntityListeners({MyEntityListener.class}) 
public class Entity extends SomeOtherEntity implements SomeInterfaceForAudit { 
} 

我EventListener的类看起来像这样

public class MyEntityListener extends EntityListener<SchoolAdmin> { 

// some other useful things in here... 

} 

我的“聪明”是,我曾经试图“泛型化”的EntityListener这样;

公共抽象类EntityListener {

public abstract class EntityListener<T> { 

     private Logger logger = LoggerFactory.getLogger(this.getClass()); 

     @PostUpdate 
     @PostConstruct 
     @PostRemove 
     public void queueForIndex(T entity) { 
      logger.info("queueForIndex " + entity.toString()); 
     } 
    } 
} 

没有做记录。我试图在我的Entity类中创建一个像这样的方法

@PostUpdate 
public void reIndex() { 
    System.out.println("--- post update-- ---- -<<<--- " + entity); 
} 

This works。我看不出为什么我的Generified versjon不应该工作?任何人?

回答

1

如果你看官方文档,对于jpa实体生命周期回调注释,你会看到那里没有@Inherited注释。这个元注释使注释从超类继承。由于它不在这些回调方法注释中,因此子类将不知道它们在超类中的存在。在你的情况下,MyEntityListener.class正在通过继承作为普通方法获取queueForIndex(T实体)方法,而不是实体生命周期回调方法。

仅显示一个如实施例参考: http://docs.oracle.com/javaee/6/api/javax/persistence/PostUpdate.html

@Target(value=METHOD) 
@Retention(value=RUNTIME) 
public @interface PostUpdate