I'm developing an application in the MVC layer pattern.
In a part of the code where I noticed a GET query, from RecordRepository using the COALESCE function, it is returning the following error in the console, as shown in the image below. I'm configuring this function, because in Postgres it doesn't just work (:min IS NULL).
org.h2.jdbc.JdbcSQLNonTransientException: Unknown data type: "NULL, ?"; SQL statement: select record0_.id as id1_2_, record0_.age as age2_2_, record0_.game_id as game_id5_2_, record0_.moment as moment3_2_, record0_.name as name4_2_ from tb_record record0_ where (coalesce(?, null) is null or record0_.moment>=?) and (coalesce(?, null) is null or record0_.moment<=?) order by record0_.moment desc limit ? [50004-214]
My code:
@Repository
public interface RecordRepository extends JpaRepository<Record, Long>{
@Query("SELECT obj FROM Record obj WHERE "
+ "(COALESCE(:min, null) IS NULL OR obj.moment >= :min) AND "
+ "(COALESCE(:max, null) IS NULL OR obj.moment <= :max)")
Page<Record> findByMoments(Instant min, Instant max, Pageable pageable);
}
I had the very same problem: COALESCE works with Postgress and it crashs the same exception with H2 database, so I created a workaround implementing two repositories interfaces (Product entity) and used annotation @profile, then in compilation time the correct query will be built. First of all I created a base repository interface without extends no other interface:
Then I created two other interfaces (one for each profile), one for development with postgres db (dev):
And another for test (where H2 database is used):
Note that my problematic query was associated with
findCustomizedmethod, but in the base interface you must declare any other repository ,method you use in the Product entity service layer, otherwise when running the code it will crash as these functions are not defined in the base interface.Also in the service layer you need to declare the base interface
Another note: It seems that Spring Boot has tagged COALESCE as deprecated, a colegue of mine tested for Spring Boot 2.4.4 the query with COALESCE with H2 and it worked fine, but as I´m using 2.7.3 it raises the exception. Anyway the query writen in @Profile("test") worked fine for both databases (H2 and postgres).