JPA @EntityListener does not work as expected

佐手、 提交于 2019-12-02 07:32:21

Although you did neither post the concrete / derived entity nor the business code to persist it, the code you posted seems correct.

For giving it a small test I added a generated UID to the super class and created a concrete entity:

import javax.persistence.Entity;

@Entity
public class DerivedEntity extends BaseEntity {

    private static final long serialVersionUID = -6441043639437893962L;

}

And since you mentioned Spring, here is a Spring Data JPA repository to save it:

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

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

}

This small test should show that the (@PrePersist) listener works:

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

}

And just to mention it, if you don't want to enhance your custom listener in future, the existing Spring Data JPA AuditingEntityListener does exactly what you are doing at the moment (and even more). In this case you could just enhance a @Configuration class with @EnableJpaAuditing and modify your BaseEntity as following:

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

    // ...
}

That would make your custom EntityListener dispensable.

Just take a look Spring JPA Auditing for more information. If you want to enhance auditing with Hibernate, try Hibernate Envers.

Thanks very much for everyone. I have resolved this problem. I will share my solution, hope it's helpful for you if you are doing same things.

First, my starting point is wrong. Because I use JPA before, so I use acquiescently @EntityListener annotation when I integrate Spring4 and Hibernate5. Then, I read Hibernate doc and many relevant article and found there is a new way to implement entity listener. Link this, enter link description here

Finally, my solution is following.

This is my BaseEntity class.

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

First of all, you need to define EntityListener class.

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

Last, you should register entity event listener.

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

}

I ran into this same issue and in my case the listener defined with @EntityListeners was referring to class (not in the same classloader) in another package and it wasn't being scanned. After adding the class to my persistence context it began working as expected.

So always be sure that any classes related to the persistence are added to the persistence context.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!