2016-11-19 10 views
3

어플리케이션 용 HSQL 임베디드 DB로 스프링 부트를 구성했습니다.org.hibernate.HibernateException : HSQL DB로 구성된 CurrentSessionContext가 없습니다

그러나 현재 세션이 구성되어 있지 않음을 나타내는 sessionFactory.getCurrentSession()을 사용할 수 없습니다.

치어 종속성

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <!-- database --> 
    <dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
    </dependency> 

dbconfig

@Configuration 
@EnableTransactionManagement 
public class DatabaseConfig { 
    @Bean 
    @Primary 
    public DataSource dataSource() { 
     final SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); 
     dataSource.setDriver(new org.hsqldb.jdbcDriver()); 
     dataSource.setUrl("jdbc:hsqldb:db/HSQL_DB/app;shutdown=true"); 
     dataSource.setUsername("sa"); 
     dataSource.setPassword("sa"); 
     return dataSource; 
    } 

    @Bean 
    public JpaVendorAdapter jpaVendorAdapter() { 
     HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 
     jpaVendorAdapter.setGenerateDdl(true); 
     jpaVendorAdapter.setShowSql(true); 
     jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect"); 
     return jpaVendorAdapter; 
    } 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 
     lef.setPackagesToScan("com.gvj.samanolsavam.entity"); 
     lef.setDataSource(dataSource()); 
     lef.setJpaVendorAdapter(jpaVendorAdapter()); 
     Properties properties = new Properties(); 
     properties.setProperty("hibernate.show_sql", "true"); 
     properties.setProperty("hibernate.jdbc.fetch_size", "100"); 
     properties.setProperty("hibernate.hbm2ddl.auto", ""); 
     lef.setJpaProperties(properties); 
     return lef; 
    } 


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

} 

내가 세션 공장을 자동 유선 및 현재 세션이 세션 팩토리를 사용합니다.

@Autowired 
    private SessionFactory sessionFactory; 

public Session getSession() { 
      return sessionFactory.getCurrentSession(); 
    } 

예외 사용자 정의 사용 확인이 설정을 config (설정) 해달라고하면 사용자의 설정 이 current_session_context_class를 추가하지만 에

properties.setProperty("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext"); 

을 추가합니다 getCurrentSession 을 사용

Caused by: org.springframework.orm.jpa.JpaSystemException: No CurrentSessionContext configured!; nested exception is org.hibernate.HibernateException: No CurrentSessionContext configured! 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:314) 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:222) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy56.findByUserNameAndPassword(Unknown Source) 
    at com.gvj.samanolsavam.services.impl.UserAccountServiceImpl.findByUserNameAndPassword(UserAccountServiceImpl.java:19) 
    at com.gvj.samanolsavam.controller.LoginController.onLoginClick(LoginController.java:91) 
    ... 58 more 
Caused by: org.hibernate.HibernateException: No CurrentSessionContext configured! 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012) 
    at com.gvj.samanolsavam.repository.impl.GenericDAOImpl.getSession(GenericDAOImpl.java:68) 
    at com.gvj.samanolsavam.repository.impl.UserAccountRepositoryImpl.findByUserNameAndPassword(UserAccountRepositoryImpl.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    ... 67 more 
+0

메소드가 @transactional에서 호출됩니까? –

+0

예, 서비스 클래스 수준에서 트랜잭션을 추가했습니다. – boycod3

답변

1

application.yml jpa :

 properties: 
      hibernate: 
       dialect : org.hibernate.dialect.MySQL5Dialect 
       show_sql : true 
       current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext 
+0

같은 오류가 발생하지 않습니다 – boycod3

+0

properties.setProperty ("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext"); hibernate.current 사이에서만 공간을 피하고 작동 중임 – Dan

+0

@ boycod3 다시 확인 –