org.hibernate.HibernateException:没有使用 HSQL DB 配置 CurrentSessionContext

Posted

技术标签:

【中文标题】org.hibernate.HibernateException:没有使用 HSQL DB 配置 CurrentSessionContext【英文标题】:org.hibernate.HibernateException: No CurrentSessionContext configured with HSQL DB 【发布时间】:2016-11-19 05:01:18 【问题描述】:

我已经为应用程序配置了带有 HSQL 嵌入式 DB 的 Spring Boot。

但我不能使用 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>

数据库配置

@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

【问题讨论】:

你的方法是在@transactional 中调用的? 是的,我已经在服务类级别添加了事务。 【参考方案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

【讨论】:

properties.setProperty("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");只在 hibernate.current 和它工作之间避免空格

以上是关于org.hibernate.HibernateException:没有使用 HSQL DB 配置 CurrentSessionContext的主要内容,如果未能解决你的问题,请参考以下文章