Hibernate native query optional parameter throws 'operator does not exist: bigint = bytea'

I have a request:

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

The parameter paramis optional, so it can benull

  • I created javax.persistance.Query
  • To which I then setParameter("param", null)
  • And when I called getResultList(), I got the following error:

Called: org.hibernate.exception.SQLGrammarException: ERROR: statement does not exist: bigint = bytea

how can i handle this?

+4
source share
2 answers

HQL and Criteria can only work when specifying the actual Entity property / Table column, so this does not work:

:param IS NULL

If id_or_smth is a column of Table1, then your query should look like this:

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();

paramValue .

SQL IS NULL/IS NOT NULL, :

SELECT id FROM table1 WHERE id_or_smth = NULL

, , id_or_smth IS NULL

+1

, :

Criteria crit = sess.createCriteria(Table1.class);
crit.setProjection(Restrictions.id());
if (param != null) crit.add(Restrictions.eq("id_or_smth", param));

List result = crit.list();
0

All Articles