您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Spring Boot-休眠会话工厂的句柄

Spring Boot-休眠会话工厂的句柄

您可以使用以下方法完成此操作:

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

其中,entityManagerFactory是一个JPA EntityManagerFactory

package net.andreaskluth.hibernatesample;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SomeService {

  private SessionFactory hibernateFactory;

  @Autowired
  public SomeService(EntityManagerFactory factory) {
    if(factory.unwrap(SessionFactory.class) == null){
      throw new NullPointerException("factory is not a hibernate factory");
    }
    this.hibernateFactory = factory.unwrap(SessionFactory.class);
  }

}
Java 2022/1/1 18:17:05 有379人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶