org.hibernate.HibernateException: 没有配置 CurrentSessionContext [英] org.hibernate.HibernateException: No CurrentSessionContext configured

查看:32
本文介绍了org.hibernate.HibernateException: 没有配置 CurrentSessionContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Web 应用程序中使用 Spring Transactional 和 AspectJ.应用上下文.xml

Iam using Spring Transactional and AspectJ in my web application. application-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mfdb1_2?useLegacyDatetimeCode=false"/>
        <property name="username" value="myfashions"/>
        <property name="password" value="myfashions123"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <context:annotation-config/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="  com.myfashions.dao, com.myfashions.handler, com.myfashions.api"/>
..
..

hibernate.cfg.xml:

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mfdb1_2?useLegacyDatetimeCode=false
        </property>
        <property name="connection.username">myfashions</property>
        <property name="connection.password">myfashions123</property>-->

        <!-- BonceCP connection pool -->
        <property name="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider
        </property>
        <property name="bonecp.connectionTestStatement">/*ping*/select 1</property>
        <property name="bonecp.setIdleMaxAgeInMinutes">60</property>
        <property name="bonecp.setIdleConnectionTestPeriodInMinutes">5</property>
        <property name="bonecp.partitionCount">3</property>
        <property name="bonecp.acquireIncrement">2</property>
        <property name="bonecp.maxConnectionsPerPartition">5</property>
        <property name="bonecp.minConnectionsPerPartition">2</property>
        <property name="bonecp.statementsCacheSize">50</property>
        <property name="bonecp.releaseHelperThreads">3</property>


        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <!--<property name="current_session_context_class">thread</property>-->
</session-factory>

</hibernate-configuration>

方面方法:

@Around(value = "@within(com.myfashions.secured.Secured) || @annotation(com.myfashions.secured.Secured)")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("method name : " + joinPoint.getSignature().getName());
        System.out.println("method arguments : " + Arrays.toString(joinPoint.getArgs()));
         try {
            // continue on the intercepted method
            Object returnObject = joinPoint.proceed();
            return returnObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

当我调用实现方法时,我得到 org.hibernate.HibernateException: No CurrentSessionContext configured! 异常.能否请您指出我哪里出错了.

When I call implementation method, I get org.hibernate.HibernateException: No CurrentSessionContext configured! exception. Can you please mention where I was gone wrong.

推荐答案

您已经在 application-context.xml 中设置了 sessionFactory 但在 hibernate.cfg.xml 中您还没有为 session 设置属性

You have set sessionFactory in application-context.xml but in hibernate.cfg.xml you have not set property for session

设置属性:::

<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>

您可以参考以下链接:::

you can refer below links:::

为什么我得到 org.hibernate.HibernateException: 没有配置 CurrentSessionContext

http://www.coderanch.com/t/559930/ORM/databases/org-hibernate-HibernateException-CurrentSessionContext-configured

这篇关于org.hibernate.HibernateException: 没有配置 CurrentSessionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆