繁体   English   中英

Spring Boot休眠拦截器

[英]Spring boot hibernate interceptor

我需要为每个数据库插入创建数据库日志事件,但在Hibernate拦截器中sessionfactory为null。 使用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()......

您可以通过注入@PersistenceContext private EntityManager entityManager;来进入休眠会话@PersistenceContext private EntityManager entityManager; 然后调用entityManager.unwrap(Session.class)

暂无
暂无

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

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