我需要为每个数据库插入创建数据库日志事件,但我的Hibernate拦截器中的sessionfactory为null。 在使用spring boot时,在拦截中获取hibernate会话的最简单方法是什么。
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()......
答案 0 :(得分:0)
您可以通过注入@PersistenceContext private EntityManager entityManager;
然后调用entityManager.unwrap(Session.class)
来获取休眠会话