Spring Boot - 处理Hibernate SessionFactory [英] Spring Boot - Handle to Hibernate SessionFactory
本文介绍了Spring Boot - 处理Hibernate SessionFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何获得由Spring Boot创建的Hibernate SessionFactory的句柄吗?
解决方案
您可以完成此操作与:
$ p $ SessionFactory sessionFactory =
其中entityManagerFactory是JPA EntityManagerFactory
。
package net.andreaskluth.hibernatesample;
导入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;
$ b $ @Autowired
public SomeService(EntityManagerFactory factory){
if(factory.unwrap(SessionFactory.class)== null){
throw new NullPointerException(factory不是休眠工厂);
}
this.hibernateFactory = factory.unwrap(SessionFactory.class);
}
}
Does anyone know how to get a handle the Hibernate SessionFactory that is created by Spring Boot?
解决方案
You can accomplish this with:
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
where entityManagerFactory is an 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);
}
}
这篇关于Spring Boot - 处理Hibernate SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文