繁体   English   中英

Spring Boot - Hibernate SessionFactory 的句柄

[英]Spring Boot - Handle to Hibernate SessionFactory

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

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

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

}

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

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

应用程序属性:

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

配置类:

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

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

@Autowired
private SessionFactory sessionFactory;

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

应用程序属性:

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

配置类:

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

伟大的工作安德烈亚斯。 我创建了一个 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);
}

它适用于 Spring Boot 2.1.0 和 Hibernate 5

@PersistenceContext
private EntityManager entityManager;

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

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

    throw new NullPointerException();
}

示例创建查询:

session.createQuery("FROM Student");

应用程序属性:

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

另一种类似于yglodt的方式

在 application.properties 中:

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

在您的配置类中:

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

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

@Autowired
private SessionFactory sessionFactory;

如果确实需要通过@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);
} }

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

其中entityManagerFactory是 JPA EntityManagerFactory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM