標籤:
非常詭異的報錯,資訊如下:
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[......]