PreInsert and PreUpdate Event Listener in hibernate

前端 未结 3 1856
有刺的猬
有刺的猬 2021-02-06 18:04

I have used PreInsertEventListener and PreUpdateEventListener Event Listener to insert created date and updated date in table.
The problem i am fac

3条回答
  •  孤城傲影
    2021-02-06 18:42

    I recently get same problem when I integrate Spring4 and Hibernate5. 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;
        }
    }
    

    Then, 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);
        }
    
    }
    

    It worked very well, I hope it's helpful for you.

提交回复
热议问题