스프링 부트 - 핸들을 휴지 상태로 전환 Session Factory
Spring Boot에 의해 작성된 Hibernate Session Factory를 처리하는 방법을 아는 사람이 있습니까?
이것은, 다음의 방법으로 실시할 수 있습니다.
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);
}
}
휴지 상태 Session Factory를 자동 배선하는 가장 간단하고 간단한 방법은 다음과 같습니다.
다음은 하이버네이트4를 탑재한 Spring Boot 1.x의 솔루션입니다.
application.properties:
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;
Spring Boot 1.5와 휴지 상태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;
}
안드레아스 수고했어Session Factory에 자동 전력이 공급되도록 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);
}
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();
}
create 쿼리 예시
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
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();
}
그런 다음 평소처럼 서비스 내에서 Session Factory를 자동 연결할 수 있습니다.
@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);
어디에entityManagerFactoryJPA 입니다.EntityManagerFactory.
언급URL : https://stackoverflow.com/questions/25063995/spring-boot-handle-to-hibernate-sessionfactory
'programing' 카테고리의 다른 글
| 반응 쿼리:사용방법버튼 클릭 시 쿼리 (0) | 2023.03.25 |
|---|---|
| JSON을 데이터베이스로 사용할 수 있습니까? (0) | 2023.03.25 |
| 대용량 JSON 파일을 빠르고 효율적으로 로드할 수 있는 방법이 있습니까? (0) | 2023.03.25 |
| 요청된 리소스에 'Access-Control-Allow-Origin' 헤더가 없습니다(Angular).JS (0) | 2023.03.20 |
| HOC - 기능 컴포넌트 (0) | 2023.03.20 |