简体   繁体   中英

Pass null value for date in SQL query in Oracle through Hibernate

I'm having trouble passing a null value for date in my sql query. The part which causes the problem is as below:

....
WHERE (s.CREATED_DATE BETWEEN (CASE WHEN ?1 IS NULL THEN ADD_MONTHS(LAST_DAY(sysdate), -500) ELSE ?1 END) AND (CASE WHEN ?2 IS NULL THEN sysdate ELSE ?2 END))
....

When i pass a null value for date in parameters via Hibernate, i get this infamous error:

ORA-00932: inconsistent datatypes: expected DATE got BINARY

I have reviewed the previous questions on this matter in stackoverflow but the solutions can't seem to be working for me. This query is run as a native query by hibernate throught spring's @Query annotation. Is there any way to solve this problem?

EDIT: I found out that if I place the @Temporal annotation besides @Param, then spring will automatically parse the date if it's null. The problem is I won't be able to use LocalDateTime and there will only be SQL Date with no time. my repository method is like this:

@Query("The query mentioned above", nativeQuery=true)
Optional<SomeDTO> getNeededInfo(@Temporal @Param("after")Date after, @Temporal @Param("before") Date before)

If you are setting Parameters to this Query, you will need to use the 3 parameter method as such:

setParameter(1, date1, TemporalType.DATE);
setParameter(2, date2, TemporalType.DATE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM