JPA @EntityListener无法按预期工作

时间:2017-06-17 08:11:19

标签: spring hibernate jpa entitylisteners

我正在集成Spring4和Hibernate5,但是有一个我无法解决的问题。 我在BaseEntity类上使用@EntityListener注释,该类是其他业务模型的超类。 我也在BaseEntity上使用@MappedSuperclass。 但它不起作用!

使用Spring base注释并成功运行应用程序。 我也向db插入了一条记录。 所以我认为我的项目配置是最新的。

任何身体让我知道为什么? 非常感谢。

这是BaseEntity类。

@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;
    }
}

这是EntityListener类。

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());
  }

}

以下是基于Spring注释的项目配置。

@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();
    }

}

关于DAL的主要配置如下,

@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;
    }

}

我使用Hibernate的LocalSessionFactoryBean类而不是JPA的EntityManager类。我想知道是不是这个原因?

--------------- 6.19 --------------

我错了。我不应该在Spring LocalSessionFactoryBean类上使用@EntityListener注释。 对于hibernate5,有一种特殊的配置方式。 http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-entitylisteners 现在,我修改我的代码如下,

@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);
    }

}

PreInsertEntityListener

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;
    }
}

但是,我还有另外一个问题。 我阅读了hibernate doc并搜索了许多有关此内容的信息。当我插入实体数据时,我的代码不起作用。

请帮助我,谢谢!

3 个答案:

答案 0 :(得分:1)

虽然您既未发布具体/派生实体也未发布业务代码以保留它,但您发布的代码似乎是正确的。

为了给它一个小测试,我将一个生成的UID添加到超类并创建了一个具体的实体:

import javax.persistence.Entity;

@Entity
public class DerivedEntity extends BaseEntity {

    private static final long serialVersionUID = -6441043639437893962L;

}

自从你提到Spring之后,这里有一个Spring Data JPA存储库来保存它:

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DerivedEntityRepository extends CrudRepository<DerivedEntity, Long> {

}

这个小测试应该表明(@PrePersist)监听器有效:

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class DerivedEntityRepositoryTests {

    @Autowired
    private DerivedEntityRepository derivedEntityRepository;

    @Test
    public void insertDerivedEntity() {
        DerivedEntity entity = new DerivedEntity();
        entity = derivedEntityRepository.save(entity);
        assertThat(entity.getCreateDate()).isNotNull();
    }

}

就是提一下,如果你以后不想增强自定义监听器,现有的Spring Data JPA AuditingEntityListener就会完成你现在正在做的事情(甚至更多)。在这种情况下,您只需使用@Configuration增强@EnableJpaAuditing课程,然后修改BaseEntity,如下所示:

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {

    // ...

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private Date createDate;

    @LastModifiedDate
    @Column(nullable = false)
    private Date modifyDate;

    // ...
}

这将使您的自定义EntityListener可有可无。

请查看Spring JPA Auditing了解详情。如果您想使用Hibernate增强审核,请尝试Hibernate Envers

答案 1 :(得分:0)

非常感谢大家。我已经解决了这个问题。 我将分享我的解决方案,希望如果你做同样的事情,它对你有帮助。

首先,我的出发点是错误的。因为我以前使用JPA,所以当我集成Spring4和Hibernate5时,我默认使用@EntityListener注释。 然后,我阅读了Hibernate doc和许多相关文章,发现有一种实现实体监听器的新方法。链接这个, enter link description here

最后,我的解决方案正在跟进。

这是我的BaseEntity类。

@MappedSuperclass
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;
    }
}

首先,您需要定义EntityListener类。

public class EntityListener implements PreInsertEventListener, PreUpdateEventListener {

    private static final String CREATE_DATE_PROPERTY = "createDate";

    private static final String MODIFY_DATE_PROPERTY = "modifyDate";

    @Override
    public boolean onPreInsert(PreInsertEvent event) {

        if (event.getEntity() instanceof BaseEntity){
            //property name of entity
            String[] propertyNames = event.getPersister().getEntityMetamodel().getPropertyNames();
            //property value of entity
            Object[] state = event.getState();
            for (int i = 0; i < propertyNames.length ; i ++) {
                if (CREATE_DATE_PROPERTY.equals(propertyNames[i]) || MODIFY_DATE_PROPERTY.equals(propertyNames[i])){
                    state[i] = new Date();
                }
            }
        }

        return false;
    }

    @Override
    public boolean onPreUpdate(PreUpdateEvent event) {

        if (event.getEntity() instanceof BaseEntity){
            //property name of entity
            String[] propertyNames = event.getPersister().getEntityMetamodel().getPropertyNames();
            //property value of entity
            Object[] state = event.getState();
            for (int i = 0; i < propertyNames.length ; i ++) {
                if (MODIFY_DATE_PROPERTY.equals(propertyNames[i])){
                    state[i] = new Date();
                }
            }
        }

        return false;
    }
}

最后,您应该注册实体事件监听器。

@SuppressWarnings("unchecked")
@Component
public class EntityEventListenerRegistry {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * EventListenerRegistry:http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-entitylisteners
     */
    @PostConstruct
    public void registerListeners(){
        EventListenerRegistry eventListenerRegistry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
        eventListenerRegistry.prependListeners(EventType.PRE_INSERT, EntityListener.class);
        eventListenerRegistry.prependListeners(EventType.PRE_UPDATE, EntityListener.class);
    }

}

答案 2 :(得分:0)

我遇到了同样的问题,在我的情况下,使用@EntityListeners定义的监听器指的是另一个包中的类(不在同一个类加载器中)并且没有被扫描。将类添加到我的持久化上下文后,它开始按预期工作。

因此,请始终确保将与持久性相关的任何类添加到持久性上下文中。