我有一个查询如下:
SELECT id FROM table1 WHERE (:param IS NULL OR id_or_smth = :param)
param
参数是可选的,因此它可以是null
javax.persistance.Query
setParameter("param", null)
时,我收到以下错误:引起:org.hibernate.exception.SQLGrammarException:错误:运算符不存在:bigint = bytea
我怎么处理这个?
只有在指定实际的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的行,也将始终返回空结果
也许你可以使用SELECT id FROM table1 WHERE id_or_smth = NULL
代替:
criteria queries