복붙노트

[SPRING] Spring Boot - SessionFactory를위한 Hibernate 핸들

SPRING

Spring Boot - SessionFactory를위한 Hibernate 핸들

누구든지 봄 부팅으로 생성 된 Hibernate SessionFactory를 처리하는 방법을 알고 있습니까?

해결법

  1. ==============================

    1.다음을 통해이를 수행 할 수 있습니다.

    다음을 통해이를 수행 할 수 있습니다.

    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);
      }
    
    }
    
  2. ==============================

    2.Hibernate SessionFactory를 autowire하는 가장 간단하고 최소한의 방법은 다음과 같다.

    Hibernate SessionFactory를 autowire하는 가장 간단하고 최소한의 방법은 다음과 같다.

    이것은 Hibernate 4와 함께 Spring Boot를위한 솔루션이다 :

    application.properties:

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

    구성 클래스 :

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

    그런 다음 평소대로 서비스에서 SessionFactory를 autowire 할 수 있습니다.

    @Autowired
    private SessionFactory sessionFactory;
    

    Hibernate 5를 사용하는 Spring Boot 1.5에서, 이것은 이제 선호되는 방법이다.

    application.properties:

    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;
    }
    
  3. ==============================

    3.위대한 일 Andreas. SessionFactory가 autowired 될 수 있도록 bean 버전을 만들었습니다.

    위대한 일 Andreas. SessionFactory가 autowired 될 수 있도록 bean 버전을 만들었습니다.

    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);
    }
    
  4. ==============================

    4.yglodt와 비슷한 또 다른 방법

    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를 autowire 할 수 있습니다.

    @Autowired
    private SessionFactory sessionFactory;
    
  5. ==============================

    5.그것은 봄 부팅 2.1.0 및 최대 절전 모드 5와 함께 작동합니다

    그것은 봄 부팅 2.1.0 및 최대 절전 모드 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");
    

    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
    
  6. from https://stackoverflow.com/questions/25063995/spring-boot-handle-to-hibernate-sessionfactory by cc-by-sa and MIT license