gpt4 book ai didi

java - 无法使用 HQL 在 Hibernate 中获取结果集

转载 作者:行者123 更新时间:2023-12-01 10:09:51 31 4
gpt4 key购买 nike

我正在使用 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,因为没有传递任何记录 w.r.t userId。

我需要做哪些改变?

最佳答案

假设 userId 的值为“abc12”

根据您的代码,名为 hql 的字符串的值将变为:“来自 com.pck.Person,其中 userId = abc12”

如果您获取该字符串的值并尝试将其作为查询在任何数据库上运行,大多数人将无法理解 abc12 是一个字符串。通常它会被解释为一个变量。

正如其他用户提到的,包含单引号会产生所需的查询,但分配参数值的推荐方法是:

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

关于java - 无法使用 HQL 在 Hibernate 中获取结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194389/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com