簡體   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