PostgreSQL JDBC Null String taken as a bytea

后端 未结 9 592
后悔当初
后悔当初 2020-12-10 02:59

If entity.getHistory() is null following code snippet:

(getEntityManager() returns spring injected EntityManager, database field history type is: text or varchar2(20

9条回答
  •  眼角桃花
    2020-12-10 03:22

    You can cast the parameter to a proper type in the query. I think this solution should work for other databases, too.

    Before:

    @Query("SELECT person FROM people person"
         + " WHERE (?1 IS NULL OR person.name = ?1)")
    List getPeopleWithName(final String name);
    

    After:

    @Query("SELECT person FROM people person"
         + " WHERE (?1 IS NULL OR person.name = cast(?1 as string))")
    List getPeopleWithName(final String name);
    

    Also works for LIKE statements:

    person.name LIKE concat('%', cast(?1 as string), '%')
    

    and with numbers (here of type Double):

    person.height >= cast(cast(?1 as string) as double)
    

提交回复
热议问题