JPA Native Query set null parameter

You are using postgresql (already the stack is telling that), and likely Hibernate, and almost certainly hitting this problem: PostgreSQL JDBC Null String taken as a bytea

I used this particular solution: https://stackoverflow.com/a/23501509/516188

So that means escaping to the Hibernate API so you can give the type of the expression.

In my case it was a nullable Short so I used:

.setParameter("short", shortValue, ShortType.INSTANCE);

shortValue being of type Short.


I have faced the same issue when use EntityManager.createNamedQuery(guess the same issue with createNativeQuery).

In case you are going to pass nullable parameter to Query then use TypedParameterValue which allows to pass the type.

For instance:

setParameter("paramName", new TypedParameterValue(StandardBasicTypes.LONG, paramValue));

Here you explicitly set the type of passed value and when you pass null value as processor know the exact type.

Tags:

Java

Jpa