Hibernate本机查询可选参数throws'运算符不存在:bigint = bytea'

问题描述 投票:4回答:2

我有一个查询如下:

SELECT id FROM table1 WHERE (:param IS NULL OR id_or_smth = :param)

param参数是可选的,因此它可以是null

  1. 我创造了一个qazxsw poi
  2. 我然后javax.persistance.Query
  3. 当我打电话给setParameter("param", null)时,我收到以下错误:

引起:org.hibernate.exception.SQLGrammarException:错误:运算符不存在:bigint = bytea

我怎么处理这个?

java sql hibernate jpa orm
2个回答
1
投票

只有在指定实际的Entity属性/ Table列时,HQL和Criteria才能工作,因此这不起作用:

getResultList()

如果id_or_smth是Table1列,那么这就是您的查询的样子:

:param IS NULL

并且paramValue不能为null。

在SQL中,您必须始终使用IS NULL / IS NOT NULL,因为这样的查询:

Query q = entityManager.createNativeQuery("SELECT id FROM table1 WHERE id_or_smth IS NULL or id_or_smth = :param");
q.setParameter("param", paramValye);
q.getResultList();

即使存在满足id_or_smth IS NULL的行,也将始终返回空结果


0
投票

也许你可以使用SELECT id FROM table1 WHERE id_or_smth = NULL 代替:

criteria queries
© www.soinside.com 2019 - 2024. All rights reserved.