簡體   English   中英

無法使用HQL在Hibernate中獲取結果集

[英]Not able to fetch resultset in Hibernate using HQL

我正在使用HQL觸發查詢,通常它應該返回空結果集,因為它沒有任何記錄。 但是,它拋出

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)

我的代碼是

String hql = "FROM com.pck.Person where userId = " + userId;
Query query = session.createQuery(hql);         
@SuppressWarnings("unchecked")
List<Dashboard> listUserDetails = query.list(); <-- Problem here.

我期望列表大小為0,因為沒有傳遞給userId的記錄。

我需要做什么改變?

假設userId的值為“ abc12”

給定您的代碼,名為hql的字符串的值將變為:“ FROM com.pck.Person,其中userId = abc12”

如果您使用該字符串的值並嘗試在任何數據庫上將其作為查詢運行,那么大多數人將無法理解abc12是字符串。 通常,它將被解釋為變量。

正如其他用戶提到的那樣,包括單引號將產生所需的查詢,但是分配參數值的推薦方法是:

  String hql = "FROM com.pck.Person where userId = :id"
  query.setParameter("id", userId);

好像您在userid周圍缺少單引號。

嘗試使用"FROM com.pck.Person where userId = '" + userId + "'";

要么

將命名參數與query.setParameter("userid", userId);

如果無法解決問題,則發布完整的stacktrace將有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM