一下代碼純屬我的項目應用不喜勿噴。java
參考連接spring
全部攔截器以及觸發器session
http://blog.csdn.net/ljhabc1982/article/details/6319251app
Interceptor 加入到程序應用當中(配置)ide
第一步繼承攔截器this
import org.hibernate.EmptyInterceptor; import org.hibernate.type.Type; import org.springframework.context.annotation.Configuration; import java.io.Serializable; /** * Created by pw on 2016/12/16. */ @Configuration public class AuditLogInterceptor extends EmptyInterceptor { @Override public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return false; } public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { return false; } }
第二部 配置攔截器.net
spring: jpa: properties: hibernate: ejb: interceptor: com.**.AuditLogInterceptor
觸發器應用配置hibernate
第一步建立觸發器blog
import org.hibernate.HibernateException; import org.hibernate.event.spi.LoadEvent; import org.hibernate.event.spi.LoadEventListener; import org.springframework.stereotype.Component; import java.util.EventListener; /** * Created by pw on 2016/12/16. */ @Component public class MyLoadEventListener implements LoadEventListener{ public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException{ System.out.println("MyLoadEventListener -->onLoad"); } }
第二部配置觸發器繼承
import org.hibernate.event.service.spi.EventListenerRegistry; import org.hibernate.event.spi.EventType; import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.jpa.HibernateEntityManagerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.persistence.EntityManagerFactory; /** * Created by pw on 2016/12/19. */ @Component public class HibernateListenersConfigurer { @Autowired private EntityManagerFactory entityManagerFactory; @Autowired private MyLoadEventListener loadEventListener; @PostConstruct public void registerListeners() { HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) this.entityManagerFactory; SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory(); EventListenerRegistry registry = sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class); System.out.println("*******************************************************"); registry.getEventListenerGroup(EventType.LOAD).appendListener(loadEventListener); } }