Spring Boot - Hibernate SessionFactory的句柄

时间:2014-07-31 16:12:57

标签: java spring hibernate spring-boot

有谁知道如何处理Spring Boot创建的Hibernate SessionFactory?

6 个答案:

答案 0 :(得分:49)

您可以通过以下方式完成此任务:

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

}

答案 1 :(得分:15)

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

答案 2 :(得分:6)

另一种类似于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;

答案 3 :(得分:3)

它与Spring Boot 2.1.0和Hibernate 5兼容

@PersistenceContext
private EntityManager entityManager;

然后,您可以使用entityManager.unwrap(Session.class)

创建新的会话。
Session session = null;
if (entityManager == null
    || (session = entityManager.unwrap(Session.class)) == null) {

    throw new NullPointerException();
}

创建查询示例:

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

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

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

其中 entityManagerFactory 是 JPA EntityManagerFactory