Java Spring Boot - Hibernate SessionFactory 的句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25063995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:32:45  来源:igfitidea点击:

Spring Boot - Handle to Hibernate SessionFactory

javaspringhibernatespring-boot

提问by Peter

Does anyone know how to get a handle the Hibernate SessionFactory that is created by Spring Boot?

有谁知道如何获得由 Spring Boot 创建的 Hibernate SessionFactory 的句柄?

采纳答案by Andreas

You can accomplish this with:

您可以通过以下方式完成此操作:

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

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

where entityManagerFactory is an JPA EntityManagerFactory.

其中 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);
  }

}

回答by HankCa

Great work Andreas. I created a bean version so the SessionFactory could be autowired.

伟大的工作安德烈亚斯。我创建了一个 bean 版本,以便 SessionFactory 可以自动装配。

import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

....

@Autowired
private EntityManagerFactory entityManagerFactory;

@Bean
public SessionFactory getSessionFactory() {
    if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
        throw new NullPointerException("factory is not a hibernate factory");
    }
    return entityManagerFactory.unwrap(SessionFactory.class);
}

回答by yglodt

The simplest and least verbose way to autowire your Hibernate SessionFactory is:

自动装配您的 Hibernate SessionFactory 的最简单和最不冗长的方法是:

This is the solution for Spring Boot 1.x with Hibernate 4:

这是 Spring Boot 1.x with Hibernate 4 的解决方案:

application.properties:

应用程序属性:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate4.SpringSessionContext

Configuration class:

配置类:

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}

Then you can autowire the SessionFactoryin your services as usual:

然后你可以SessionFactory像往常一样在你的服务中自动装配:

@Autowired
private SessionFactory sessionFactory;

As of Spring Boot 1.5 with Hibernate 5, this is now the preferred way:

从带有 Hibernate 5 的 Spring Boot 1.5 开始,这是现在的首选方式:

application.properties:

应用程序属性:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate5.SpringSessionContext

Configuration class:

配置类:

@EnableAutoConfiguration
...
...
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
    HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
    fact.setEntityManagerFactory(emf);
    return fact;
}

回答by Lorenzo Lerate

Another way similar to the yglodt's

另一种类似于yglodt的方式

In application.properties:

在 application.properties 中:

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

And in your configuration class:

在您的配置类中:

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
    return hemf.getSessionFactory();
}

Then you can autowire the SessionFactory in your services as usual:

然后你可以像往常一样在你的服务中自动装配 SessionFactory:

@Autowired
private SessionFactory sessionFactory;

回答by Tr?n Qu?c V?

It works with Spring Boot 2.1.0 and Hibernate 5

它适用于 Spring Boot 2.1.0 和 Hibernate 5

@PersistenceContext
private EntityManager entityManager;

Then you can create new Session by using entityManager.unwrap(Session.class)

然后你可以使用 entityManager.unwrap(Session.class) 创建新的 Session

Session session = null;
if (entityManager == null
    || (session = entityManager.unwrap(Session.class)) == null) {

    throw new NullPointerException();
}

example create query:

示例创建查询:

session.createQuery("FROM Student");

application.properties:

应用程序属性:

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:db11g
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

回答by Sen

If it's really required to access SessionFactory through @Autowire, I'd rather configure another EntityManagerFactory and then use it to configure the SessionFactory bean, like following:

如果确实需要通过@Autowire 访问 SessionFactory,我宁愿配置另一个 EntityManagerFactory,然后使用它来配置 SessionFactory bean,如下所示:

@Configuration
public class SessionFactoryConfig {

@Autowired 
DataSource dataSource;

@Autowired
JpaVendorAdapter jpaVendorAdapter;

@Bean
@Primary
public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setJpaVendorAdapter(jpaVendorAdapter);
    emf.setPackagesToScan("com.hibernateLearning");
    emf.setPersistenceUnitName("default");
    emf.afterPropertiesSet();
    return emf.getObject();
}

@Bean
public SessionFactory setSessionFactory(EntityManagerFactory entityManagerFactory) {
    return entityManagerFactory.unwrap(SessionFactory.class);
} }