1. 程式人生 > >Spring+Hibernate4 Junit 報錯No Session found for current thread

Spring+Hibernate4 Junit 報錯No Session found for current thread

論壇上有另外一篇更全面的帖子,jinnianshilongnian寫的:http://www.iteye.com/topic/1120924 


本文的環境是: 
spring-framework-3.1.0 
hibernate-4.1.6 
junit-4.10 

這裡大部分是參考我以前熟悉的配置方法,只是把hibernate3升級到hibernate4,其實差不了很多,只要注意幾個要點: 

1、以前整合hibernate3和spring的時候,spring的ORM包裡提供了HibernateSupport和HibernateTemplate這兩個輔助類,我用的是HibernateTemplate。不過在Hibernate4裡,spring不再提供這種輔助類,用的是hibernate4的原生API 


2、整合hibernate4之後,最小事務級別必須是Required,如果是以下的級別,或者沒有開啟事務的話,無法得到當前的Session 
Java程式碼  收藏程式碼
  1. sessionFactory.getCurrentSession();  

執行這行程式碼,會丟擲No Session found for current thread 

對於執行時,這個可能不是很大的問題,因為在Service層一般都會開啟事務,只要保證級別高於Required就可以了。可是由於在Dao層是不會開啟事務的,所以針對Dao層進行單元測試就有困難了。 

解決的辦法是,或者在Dao層的單元測試類上,開啟事務。或者專門準備一個for unit test的配置檔案,在Dao層就開啟事務。我採用的是前者 


首先是目錄結構,這裡暫時還沒有整合struts2、spring-mvc等web框架,也尚未包含js、css、jsp等目錄 



這裡除了servlet規範規定的web.xml必須放在WEB-INF下之外,其他的所有配置檔案,都放在src根目錄下。這樣做的好處是,後續所有需要引用配置檔案的地方,都可以統一用classpath:字首找到配置檔案。之前試過有的檔案放在WEB-INF下,有的放在src根目錄下,所以在引用的地方會不太統一,比較麻煩。 

當然無論配置檔案怎麼放,只要恰當使用classpath:和file:字首,都是能找到的,只是個人選擇的問題。另外,由於現在配置檔案還比較少,所以直接扔到src根目錄下沒什麼問題,如果配置檔案增多了,可以再進行劃分 


接下來是web.xml 
Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  6.     id="WebApp_ID" version="3.0">  
  7.     <display-name>DevelopFramework</display-name>  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:beans.xml</param-value>   
  11.     </context-param>  
  12.     <listener>    
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  14.     </listener>    
  15.     <servlet>    
  16.         <servlet-name>CXFServlet</servlet-name>    
  17.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  18.         <load-on-startup>1</load-on-startup>    
  19.     </servlet>    
  20.     <servlet-mapping>    
  21.         <servlet-name>CXFServlet</servlet-name>    
  22.         <url-pattern>/webservice/*</url-pattern>    
  23.     </servlet-mapping>  
  24. </web-app>  

這裡沒有什麼要特別注意的,只是聲明瞭beans.xml的路徑。這裡的servlet是配置cxf的,與hibernate沒有關係。因為目標是要搭一個完整的開發框架,所以把cxf也事先放上了 

接下來是spring的配置檔案beans.xml 
Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.                             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.                             http://www.springframework.org/schema/context  
  10.                             http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  11.                             http://www.springframework.org/schema/tx  
  12.                             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  13.                             http://cxf.apache.org/jaxws   
  14.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  15.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  16.     <context:component-scan base-package="com.huawei.inoc.framework" />  
  17.     <bean id="propertyConfigurer"  
  18.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  19.         <property name="locations">  
  20.             <list>  
  21.                 <value>classpath:jdbc.properties</value>  
  22.             </list>  
  23.         </property>  
  24.     </bean>  
  25.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  26.         <property name="driverClass" value="${driverClass}" />  
  27.         <property name="jdbcUrl" value="${jdbcUrl}" />  
  28.         <property name="user" value="${user}" />  
  29.         <property name="password" value="${password}" />  
  30.     

    相關推薦

    Spring+Hibernate4 Junit No Session found for current thread

    論壇上有另外一篇更全面的帖子,jinnianshilongnian寫的:http://www.iteye.com/topic/1120924  本文的環境是:  spring-framework-3.1.0  hibernate-4.1.6  junit-

    Hibernate4 No Session found for current thread原因

    Hibernate4 與 spring3 整合之後, 如果在取得session 的地方使用了getCurrentSession, 可能會報一個錯:“No Session found for current thread”, 這個錯誤的原因,網上有很多解決辦法, 但具體原因的

    getCurrentSession()時報no session found for current thread,解決方案

    當我們使用spring框架,通過getCurrentSession()獲得與執行緒繫結的session時,可能會遇到no session found for current thread的錯誤,要解決該錯誤,我們必須先知道該錯誤產生的原因是什麼。 無論我們是在用Hibern

    SpringMVC3+Hibernate4問題:org.hibernate.HibernateException: No Session found for current thread

    問:1:org.hibernate.HibernateException: No Session found for current thread 解決方法: 在web.xml中新增openSessionInViewFilter <filter>

    SSH dao層異常 org.hibernate.HibernateException: No Session found for current thread

    llb exec pan val span ldr cep sch nds 解決方法: 在 接口方法中添加 事務註解 即可。 public interface IBase<PK extends Serializable, T> { @

    Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread

    背景 使用spring框架整合Hibernate的時候,通過getCurrentSession()獲得與執行緒繫結的session時,可能會遇到no session found for current thread的錯誤; 原因:呼叫getCurrentSession()之前,沒有呼

    org.hibernate.HibernateException: No Session found for current thread

    <!-- hibernate --> <filter> <filter-name>OpenSessionInViewFilter</filter-nam

    springMvc中介面返回結果轉換:No converter found for return value of type: class java.util.ArrayList

    1.在spring web服務中介面返回結果報錯問題 org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of typ

    swaggerNo handler found for GET /swagger-ui.html

    今天下載jeeweb框架下來研究,其他還有,就是swagger老是出不來。報錯:No handler found for GET /swagger-ui.html 後來搜尋才發現,這個錯誤,是因為資源對映問題導致的。 我們在訪問http://127.0.0.1:8188/swagger-ui.html

    JUnitNo tests found with test runner JUnit4”和“The input type of the launch configuration does not exist”

    【前言】不爽!!! Eclipse總是報錯: No tests found with test runner JUnit4 首先如下幾點 1 你有沒有匯入Junit包(eclipse中就自帶了)如果用到了spring的測試類有沒匯入 spring-test包 2 你的包版本

    Android studio 升級No toolchains found in the NDK toolchains folder for ABI with prefix

    ANDRID STUDIO 升級報錯 No toolchains found in the NDK toolchains folder for ABI with prefix 當出現 No toolchains found in the NDK toolchains folder for A

    No resource found that matches the given name (at 'title' with value '@string/action_settings'

    報錯 menu檔案 No resource found that matches the given name (at ‘title’ with value ‘@string/action_settings’). 解決方法 在string.xml加上<string na

    springJunit

    在用pring中的Junit做單元測試是出現下面這種錯誤。。。 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionList

    android2.3No toolchains found in the NDK toolchains folder for ABI with XX 的解決辦法

    android2.3報錯No toolchains found in the NDK toolchains folder for ABI with XX 的解決辦法 就是缺失NDK目錄下的檔案; 升級到最新版(本人舊版下了包之後還是報錯) , 在官網上下載NDK包, 放到NDK資料夾下(

    解決hibernate:no-session的問題

    問題1:因為沒有將web.xml中的openSessionInViewFilter這樣配置導致的問題,可以這樣去配置,即可解決問題。問題2:在我一次專案當中,我已經這樣配置了,但是仍然出現no-session的情況,這種情況下的解決方案是,我們要懶載入的bean是不是在載入這

    關於No typehandler found for property XXX `updatetime` timestamp NOT NULL -----的解決方法

    出了問題不可怕,要勇於面對面對問題,首先你的弄明報報的錯是啥意思 No typehandler found for property XXX:的意思就是說沒有為屬性XXX找到對應的匹配欄位  也就是說對映的時候出現了問題 一般情況下我們會看以下三個地方 1.XML檔案:檢視實體

    Xcode 10 升級專案 “directory not found for option” and “library not found for -libstdc++.6 ~解決方法

        聯絡人:石虎 QQ:1224614774   暱稱: 嗡嘛呢叭咪哄                       &

    Xcode10升級專案library not found for -lstdc++.6.0.9

    專案一直用Xcode9 進行編譯,突然之間蘋果手機自動升級到ios12了,然而xcode就編譯不了了,必須切換到xcode10. 切換到xcode10,就編譯不過去了,報以下錯: 會看到報錯:library not found for -lstdc++.6.0.9  

    ssh No result defined for action and result input

    配置如下: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2

    springboot-jpaNo identifier specified for entity:

    No identifier specified for entity:。 百度解決方案全是@Id和@GeneratedValue註解不要加在setId()方法上,要加在get方法上,試了還是未解決。 原來是@Id註解import錯包了。 錯誤: