SSH整合報錯:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

來源:互聯網
上載者:User

標籤:

非常詭異的報錯,資訊如下:
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(*) from User u where u.userName=? and u.userPassword=? ]
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped
類似的報錯資訊的解決方案,我也是研究了半天才最後發現問題
出現這中錯誤情況有兩種

1,設定檔沒有載入到hibernate的實體列表裡面

2,對應檔的欄位與資料庫欄位不一致,或者名稱不一致導致

hql 是 有專門的文法的。 不是sql。你把hibernate方言設定成你用的資料庫。
sql的文法和hql不是一回事。
......QuerySyntaxException...是說,你要的sql不是hql,語法錯誤。。

1.看是否忘記將hibernate的對應檔添加到Hibernate.cfg.xml(使用Hibernate時)或者applicationContext.xml中

2.檢查表中的欄位和對應檔中的欄位是否一一對應

3.檢查欄位名是否使用了資料庫中的關鍵字

4.HQL語句是否正確

HQL: Hibernate 查詢語言Hibernate 配備了一種非常強大的查詢語言,這種語言看上去很像 SQL。但是不要被文法結構上的相似所迷惑,HQL 是非常有意識的被設計為完全物件導向的查詢,它可以理解如繼承、多態和關聯之類的概念。

 

所以這個時候你要認真檢查一下你寫的hql語句,一定是物件查詢,特別是【tableName】 不要寫你要查詢的表,而是查詢的對象

比如:

public long getTypeCount(Patent patent) {
String hqlString = "select count(*) from Patent as p where p.type =‘"+patent.getType()+"‘";
Query query = this.getSession().createQuery(hqlString);
long count =0;
count = ((Number)query.uniqueResult()).intValue();
return count;
}

Patent  是一個對象

而資料庫的表名是patent   如果寫成String hqlString = "select count(*) from patent as p where p.type =‘"+patent.getType()+"‘"; 

肯定是查詢不出結果的,切記!

SSH整合報錯:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.