Postgres bytea error when binding null to prepared statements

前端 未结 2 811
慢半拍i
慢半拍i 2021-01-23 07:50

I am working with a Java application which uses JPA and a Postgres database, and I am trying to create a flexible prepared statement which can handle a variable number of input

相关标签:
2条回答
  • 2021-01-23 08:09

    If you want to keep using plain queries with automatic parameter binding, you could try the following.

    WHERE (? IS NULL OR (CAST(CAST(? AS TEXT) AS DOUBLE PRECISION) = double_col
    

    This seems to satisfy the PostgreSQL driver's type checks as well as yielding the correct results. I haven't done much testing, but the performance hit seems minimal because the CASTs happen on a constant value rather than rows from the database.

    0 讨论(0)
  • 2021-01-23 08:11

    Not having received any feedback in the form of answers or even a comment, I was getting ready to give up, when I stumbled onto this excellent blog post:

    How to bind custom Hibernate parameter types to JPA queries

    The post gives two options for controlling the types which JPA passes through the driver to Postgres (or whatever the underlying database actually is). I went with the approach using TypedParameterValue. Here is what my code looks like continuing with the example given above:

    Query query = entityManager.createNativeQuery(sqlString, MyEntity.class);
    query.setParameter("param1", new TypedParameterValue(StringType.INSTANCE, null));
    query.setParameter("param2", new TypedParameterValue(DoubleType.INSTANCE, null));
    List<MyEntity> = query.getResultList();
    

    Of course, it is trivial to be passing null for every parameter in the query, but I am doing this mainly to show the syntax for the text and double columns. In practice, we would expect at least a few of the parameters to be non null, but the above syntax handles all values, null or otherwise.

    0 讨论(0)
提交回复
热议问题