简体   繁体   English

Spring Boot休眠拦截器

[英]Spring boot hibernate interceptor

I need to create DB log events for every DB insert but the the sessionfactory is null in my Hibernate interceptor. 我需要为每个数据库插入创建数据库日志事件,但在Hibernate拦截器中sessionfactory为null。 What's the simplest way for getting a hibernate session in your interception when you are using spring boot. 使用Spring Boot时,在拦截中获取休眠会话的最简单方法是什么?

public class Interceptor extends EmptyInterceptor {

@Autowired
SessionFactory session; //this is null not in the spring scope

public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
 sessionFactory.getCurrentSession()......

You can get hibernate session by injecting @PersistenceContext private EntityManager entityManager; 您可以通过注入@PersistenceContext private EntityManager entityManager;来进入休眠会话@PersistenceContext private EntityManager entityManager; and then calling entityManager.unwrap(Session.class) 然后调用entityManager.unwrap(Session.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM