2016-11-19 70 views
3

配置我已經配置春天開機與應用程序HSQL嵌入式數據庫。org.hibernate.HibernateException:沒有CurrentSessionContext與HSQL DB

但我不能使用sessionFactory.getCurrentSession()顯示沒有配置當前會話。

POM依賴

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

異常

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

使用的getCurrentSession 這current_session_context_class添加到您的配置 如果你不使用自定義配置此配置好,但你會 添加

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

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檢查 –

相關問題