title: JPA @EntityListener does not work as expected
body: <p>I am integrating Spring4 and Hibernate5, but there is a problem that I can't resolve.
  I use @EntityListener annotation on the BaseEntity class that is a super class for other business model.
  Also I use @MappedSuperclass on the BaseEntity.
  But it don't work!</p>
  
  <p>Use Spring base annotation and run application successfully.
  Also I inserted a record to db.
  So I think my configuration of project is current.</p>
  
  <p>Any body let me know why?
  Thanks very much.</p>
  
  <p>This is BaseEntity class.</p>
  
  <pre><code>@MappedSuperclass
  @EntityListeners(EntityListener.class)
  public class BaseEntity implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(nullable = false, updatable = false)
      private Date createDate;
  
      @Column(nullable = false)
      private Date modifyDate;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Date getCreateDate() {
          return createDate;
      }
  
      public void setCreateDate(Date createDate) {
          this.createDate = createDate;
      }
  
      public Date getModifyDate() {
          return modifyDate;
      }
  
      public void setModifyDate(Date modifyDate) {
          this.modifyDate = modifyDate;
      }
  }
  </code></pre>
  
  <p>This is EntityListener class.</p>
  
  <pre><code>public class EntityListener {
  
    @PrePersist
    public void prePersist(BaseEntity entity) {
      entity.setCreateDate(new Date());
      entity.setModifyDate(new Date());
    }
  
    @PreUpdate
    public void preUpdate(BaseEntity entity) {
      entity.setModifyDate(new Date());
    }
  
  }
  </code></pre>
  
  <p>The following is my project configuration base on Spring annotation.</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  //@ImportResource({ "classpath:xxxxx.xml" })
  @PropertySources({
      @PropertySource("classpath:application.properties")
  })
  @ComponentScan({"com.yeager.admin.persistence","com.yeager.admin.web","com.yeager.admin.service","com.yeager.admin.common"})
  @EnableAspectJAutoProxy
  //@EnableRetry
  public class AppConfig {
  
      @Bean(name = "multipartResolver")
      public CommonsMultipartResolver getResolver() throws IOException {
          CommonsMultipartResolver resolver = new CommonsMultipartResolver();
          return resolver;
      }
  
      @Bean
      public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  
      @Bean
      public static SpringContext springContext() {
          return new SpringContext();
      }
  
  }
  </code></pre>
  
  <p>The main configuration about DAL like this,</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource({"classpath:persistence-mysql.properties"})
  public class PersistenceConfig {
  
      @Autowired
      private Environment env;
  
      public PersistenceConfig() {
          super();
      }
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
  
          final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan("com.yeager.admin.persistence.entity");
          sessionFactory.setHibernateProperties(hibernateProperties());
  
  
          return sessionFactory;
      }
  
      @Bean
      public DataSource dataSource() {
          ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
          try {
              comboPooledDataSource.setDriverClass(env.getProperty("jdbc.driver"));
          } catch (PropertyVetoException e) {
              e.printStackTrace();
          }
          comboPooledDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
          comboPooledDataSource.setUser(env.getProperty("jdbc.username"));
          comboPooledDataSource.setPassword(env.getProperty("jdbc.password"));
          comboPooledDataSource.setInitialPoolSize(Integer.valueOf(env.getProperty("datasource.pool.initialPoolSize")));
  
          return comboPooledDataSource;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(sessionFactory().getObject());
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      private final Properties hibernateProperties() {
  
          final Properties hibernateProperties = new Properties();
  
          hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
          hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
          hibernateProperties.setProperty("hibernate.generate_statistics",env.getProperty("hibernate.generate_statistics"));
          hibernateProperties.setProperty("hibernate.jdbc.fetch_size", env.getProperty("hibernate.jdbc.fetch_size"));
          hibernateProperties.setProperty("hibernate.jdbc.batch_size", env.getProperty("hibernate.jdbc.batch_size"));
          hibernateProperties.setProperty("hibernate.max_fetch_depth", env.getProperty("hibernate.max_fetch_depth"));
          hibernateProperties.setProperty("hibernate.cache.use_second_level_cache",env.getProperty("hibernate.cache.use_second_level_cache"));
          hibernateProperties.setProperty("hibernate.cache.use_query_cache",env.getProperty("hibernate.cache.use_query_cache"));
  //      hibernateProperties.setProperty("hibernate.cache.provider_class",env.getProperty("hibernate.cache.provider_class"));
          hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
  
          return hibernateProperties;
      }
  
  }
  </code></pre>
  
  <p><strong>I use LocalSessionFactoryBean class of Hibernate rather than EntityManager class of JPA. I wonder if this cause ?</strong></p>
  
  <p>--------------- 6.19 --------------</p>
  
  <p>I am wrong. I don't should use @EntityListener annotation base on Spring LocalSessionFactoryBean class.
  For hibernate5, there is a special configuration way.
  <a href="http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-entitylisteners" rel="nofollow noreferrer">http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-entitylisteners</a>
  Now, I modify my code as following,</p>
  
  <pre><code>@Component
  public class EntityEventListener {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      @PostConstruct
      public void registerListeners(){
          EventListenerRegistry eventListenerRegistry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
          eventListenerRegistry.prependListeners(EventType.PRE_INSERT, PreInsertEntityListener.class);
      }
  
  }
  </code></pre>
  
  <p>PreInsertEntityListener</p>
  
  <pre><code>public class PreInsertEntityListener implements PreInsertEventListener {
      @Override
      public boolean onPreInsert(PreInsertEvent event) {
  //        if (event.getEntity() instanceof AdminUser){
  //            ((AdminUser) event.getEntity()).setCreateDate(new Date());
  //            ((AdminUser) event.getEntity()).setModifyDate(new Date());
  //        }
          BaseEntity baseEntity = (BaseEntity) event.getEntity();
          baseEntity.setCreateDate(new Date());
          baseEntity.setModifyDate(new Date());
          return false;
      }
  }
  </code></pre>
  
  <p>But, I have a other problem.
  I read hibernate doc and search many information about this. My code don't work already when I insert entity data.</p>
  
  <p>Please help me, thanks!</p>