在org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)找不到当前线程的会话

时间:2018-02-22 04:22:16

标签: spring hibernate spring-boot

我已配置datasource并配置了sessionfactory对象,但我没有从工厂对象获取数据库会话。

例外

org.springframework.orm.hibernate4.SpringSessionContext.currentSession
(SpringSessionContext.java:106) The results were same when I used the 
below tag (uncommented ) org.apache.commons.dbcp.BasicDataSource

取代org.springframework.jdbc.datasource.DriverManagerDataSource

SpringConfig档案

< beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
" >
    <mvc:annotation-driven />
    <context:component-scan base-package="com.gcp.spring.boot.pavan_mvnspring_hibernate" />
    <tx:annotation-driven /> 
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>   

    <!--    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> -->
    <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="url" value="jdbc:derby://localhost:1527/PavanDB"/>
        <property name="username" value="gc1"/>
        <property name="password" value="gc1"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources">
            <list> <value>Employees.hbm.xml</value> </list>
        </property>
        <property name="hibernateProperties">
            <value> org.hibernate.dialect.DerbyDialect  </value>
        </property>
    </bean>

    <bean id="myEmpDao" class="com.gcp.spring.boot.pavan_mvnspring_hibernate.ManageEmployeeBean">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean id="transactionManager"
           class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

</beans>

package com.gcp.spring.boot.pavan_mvnspring_hibernate;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.hibernate.SessionFactory;
import org.springframework.web.bind.annotation.RequestMapping;


public class ManageEmployeeBean {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
   // @RequestMapping("/Data")
    public int loadProductsByCategory( ) {

        int xyz = 0;
               System.out.println("GCP123");    
       Session abc = this.sessionFactory.getCurrentSession();

         return xyz;
    }
}


<dependencies>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.10.1.1</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.14.RELEASE</version>
        <type>jar</type>
    </dependency> 

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>  
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

2 个答案:

答案 0 :(得分:0)

你的spring XML配置还不够。

请配置交易切入点。

<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
    <tx:method name="loadProductsByCategory" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

 <aop:config>
    <aop:pointcut id="dbServiceOperations"
    expression="execution(* 
  com.gcp.spring.boot.pavan_mvnspring_hibernate.ManageEmployeeBean.*(..))" />
    <aop:advisor pointcut-ref="dbServiceOperations" advice-ref="defaultTxAdvice" />
  </aop:config>

答案 1 :(得分:0)

我建议你创建一个DAO级别,并用@Repository映射特定的EmployeeDao类。并将@Transactional放入您的服务层。