Spring Boot - Hibernate SessionFactory的句柄

Pet*_*ter 51 java spring hibernate spring-boot

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

And*_*eas 55

你可以用:

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

}
Run Code Online (Sandbox Code Playgroud)

  • 如果你将`SessionFactory`的创建移动到Spring` @ Configuration`并制作一个`@ Bean`方法来创建它可能会更好. (4认同)
  • 对于社区,我在`@Bean` 中使用该代码,并且在`application.properties` 文件中以下是强制性的`spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext`,否则会出现:`org.springframework.orm.jpa.JpaSystemException:没有配置CurrentSessionContext!;嵌套异常是 org.hibernate.HibernateException:没有配置 CurrentSessionContext!` (2认同)

ygl*_*odt 48

自动装配Hibernate SessionFactory最简单,最简单的方法是:

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

application.properties:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

配置类:

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}
Run Code Online (Sandbox Code Playgroud)

然后您可以SessionFactory像往常一样自动装入您的服务:

@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)

从Spring Boot 1.5到Hibernate 5,现在这是首选方式:

application.properties:

spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate5.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

配置类:

@EnableAutoConfiguration
...
...
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
    HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
    fact.setEntityManagerFactory(emf);
    return fact;
}
Run Code Online (Sandbox Code Playgroud)

  • HibernateJpaSessionFactoryBean现在已经被删除了 (22认同)
  • 作为一个额外的说明,提到的配置调用是@SpringBootApplication你的应用程序.您可以将此方法粘贴到main方法之上. (4认同)
  • 我的 sessionFactory 返回 null。有什么建议吗? (2认同)

Han*_*kCa 15

伟大的工作Andreas.我创建了一个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);
}
Run Code Online (Sandbox Code Playgroud)


小智 10

它适用于 Spring Boot 2.1.0 和 Hibernate 5

@PersistenceContext
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)

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

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

    throw new NullPointerException();
}
Run Code Online (Sandbox Code Playgroud)

示例创建查询:

session.createQuery("FROM Student");
Run Code Online (Sandbox Code Playgroud)

应用程序属性:

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
Run Code Online (Sandbox Code Playgroud)


Lor*_*ate 6

另一种类似于yglodt的方式

在application.properties中:

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

在您的配置类中:

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
    return hemf.getSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)

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

@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)