title: PostgreSQL, Spring Data JPA: Integer null interpreted as bytea
body: <p>In PostgreSQL I have the table</p>
  
  <pre><code>CREATE TABLE public.my_table
  (
      id integer NOT NULL,
      ...
  </code></pre>
  
  <p>I want to perform the query: <em>Show me the rows with a given id. If id is null, show me all rows.</em></p>
  
  <p>I tried it with</p>
  
  <pre><code>public interface MyRepository extends JpaRepository&lt;MyTable, Integer&gt; {
  
      @Query(value = "SELECT * FROM my_table WHERE (?1 IS NULL OR id = ?1)", nativeQuery = true)
      List&lt;MyTable&gt; findAll(Integer id);
  </code></pre>
  
  <p>If <code>id != null</code>, everything is fine. But if <code>id == null</code>, I will receive the error</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  ...
  Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  ...
  Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = bytea
    Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
      at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar:42.2.5]
  ...
  </code></pre>
  
  <p>Obviously short circuit evaluation does not work and <code>null</code> is transformed into <code>bytea</code>.</p>
  
  <p>As a workaround I have changed the query value into</p>
  
  <pre><code>SELECT * FROM my_table WHERE (?1 IS NULL OR id = (CAST (CAST(?1 AS character varying) AS integer)))
  </code></pre>
  
  <p>But this is not nice, because the int is cast to string and to int again. Do you have a better solution, e.g. a better cast or sql query?</p>