sql - Using JPA with Java Spring Boot. Having Issue with optional parameter. JDBC could not determine data type - Stack Overflow

As stated in the title, I'm facing this issue when optional paramter(endDate) is null. It doesn�

As stated in the title, I'm facing this issue when optional paramter(endDate) is null. It doesn't throw any error when both parameters are provided. I tried usin cast null as timestamp with time zone, cast as timestamp to both params in the query and it throws the same error. Please advise.

@Query("""
SELECT setspanyId, COUNT(sets)
FROM WarehouseSetsEntity sets
WHERE (:endDate IS NULL AND sets.importDate >= :beginDate)
OR (:endDate  IS NOT NULL AND sets.importDate BETWEEN :beginDate  AND :endDate)
GROUP BY setspanyId""")
List<Object[]> fetchCompanyByDateRange(@Param("beginDate")  OffsetDateTime beginDate,  @ Param("endDate") @NullableOffsetDateTime endDate);
Error:.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [select wse1_0panyid,count(wse1_0.setid) from sets wse1_0 where (coalesce(?,'1970-01-01T00 00:00Z') is null and wse1_0.import_date>=?) or (? is not null and wse1_0.import_date between ? and ?) group by wse1_0panyid] [ERROR: could not determine data type of parameter $3] [n/a]; SQL [n/a]

As stated in the title, I'm facing this issue when optional paramter(endDate) is null. It doesn't throw any error when both parameters are provided. I tried usin cast null as timestamp with time zone, cast as timestamp to both params in the query and it throws the same error. Please advise.

@Query("""
SELECT setspanyId, COUNT(sets)
FROM WarehouseSetsEntity sets
WHERE (:endDate IS NULL AND sets.importDate >= :beginDate)
OR (:endDate  IS NOT NULL AND sets.importDate BETWEEN :beginDate  AND :endDate)
GROUP BY setspanyId""")
List<Object[]> fetchCompanyByDateRange(@Param("beginDate")  OffsetDateTime beginDate,  @ Param("endDate") @NullableOffsetDateTime endDate);
Error:.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [select wse1_0panyid,count(wse1_0.setid) from sets wse1_0 where (coalesce(?,'1970-01-01T00 00:00Z') is null and wse1_0.import_date>=?) or (? is not null and wse1_0.import_date between ? and ?) group by wse1_0panyid] [ERROR: could not determine data type of parameter $3] [n/a]; SQL [n/a]
Share edited Mar 10 at 23:53 ilaunchpad asked Mar 10 at 23:44 ilaunchpadilaunchpad 1,3333 gold badges17 silver badges32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Can you try using below code?

WHERE sets.importDate >= :beginDate AND (sets.importDate <= :endDate OR :endDate IS NULL)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744819498a4595529.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信