1. 程式人生 > 其它 >s1sh(struts1+spring+hibernate)整合過程中遇一些的問題

s1sh(struts1+spring+hibernate)整合過程中遇一些的問題

技術標籤:javaspringhibernatemavenmysql

1. 使用hql語句時的報錯: org.hibernate.hql.internal.ast.QuerySyntaxException: USER is not mapped 報錯解決
原因:
這個錯誤的原因在於from後面接的應該是類名(User),而不是表名(USER)。
我的hql語句是
String hql = “from user”;
解決:
所以正確的寫法是String hql = “from User”

2. 在hbm.xml中的table名報錯: Cannot resolve table 根據提示assign data Source,發現dataSource中提示default or no dataSource


原因:
經過檢查發現是我之前更改了jdbc的驅動但之前那個沒有刪除,導致一直引入的都是錯誤的驅動
解決:
參照https://www.cnblogs.com/yinze/p/13894069.html

3. 配置檔案中的報錯:Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/action-servlet.xml]
原因:
原因在於contextConfigLocation屬性是否配置寫錯了
解決:
檢查struts-config.xml

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="ContextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

ContextConfigLocation的頭字母“C"是大寫的。改成小寫,重啟系統。異常資訊沒有了。

4. applicationContext.xml中的sessionFactory配置錯誤導致:Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property ‘dataSource’: no matching editors or conversion strategy found
原因:
在applicationComtext.xml中dataSource的引用錯誤


解決:
把value改成ref即可

5. Spring注入一直失敗,bean寫的也沒有問題
可能原因:
在struts-config.xml檔案中配置action時沒有把actionBean的建立交給spring來管理
解決:
將type的值改為以下即可
type=“org.springframework.web.struts.DelegatingActionProxy”

6. 使用hibernateTemplate報錯 getsession方法找不到
原因:
import的hibernate版本(hibernate3)與pom中匯入的hibernate版本(hibernate4)不一致
hibernateTemplete只有在hibernate3以下支援,在hibernate4以上不支援,
原因是hibernate4已經開始支援事務管理會與hibetnateTemplete衝突
解決:
在pom.xml中將hibernate版本更改為hibernate3.X(我改成了3.5.1-Final)即可