如何在休眠nativeQuery中投射空日期? [英] How to cast a null date in an hibernate nativeQuery?
问题描述
我正在使用postgreSQL和休眠模式.因为必须使用休眠不考虑的SQL查询,所以我在Query批注中使用nativeQuery.我有多个日期参数,并且不可避免地至少有两个为空.
I'm using postgreSQL and hibernate. Because I've to use SQL queries that is not take into account by hibernate, I use nativeQuery in my Query annotation. I have multiple dates param and inevitably at least two of them are null.
仅当我的参数不为null时,我才需要检查条件.问题是,如果此日期为空,我将无法进行约会.
I have to check a condition only if my param isn't null. The problem is that I'm not able to cast my date if this one is null.
我尝试了其他可能的解决方案,但没有任何效果
I try different possible solutions but nothing works
-
:dEchTransacEqual为null或transaction.d_ech_transac = cast(:dEchTransacEqual as date)
coalesce(:dEchTransacGreaterThanEqual,null)为null或transaction.d_ech_transac> = cast(:dEchTransacGreaterThanEqual as date)
我没有更多的想法来解决我的问题.如何使用hibernate的nativeQuery处理日期的无效性?
I don't have more idea to solve my problem. How can I handle the nullity of my date using the nativeQuery of hibernate?
最后一种(也是肮脏的)解决方案是实现与我的用例(3)一样多的repo方法.
The last (and dirty) solution is to implement as many repo method as I have use cases (3).
PS.:我必须使用nativeQuery,因为我使用的是over(),因此Hibernate无法处理分区.
PS. : I have to use nativeQuery because I'm using over() and partition by that isn't handle by Hibernate.
推荐答案
似乎PG不支持参数的"IS NULL"测试.这意味着您必须动态地构建查询.如果您不想操纵字符串,则可以使用FluentJPA的动态查询,它还支持 OVER
子句
It looks like PG does not support "IS NULL" test for parameters. It means that you must build a query dynamically. If you don't want manipulate strings, you may use FluentJPA's Dynamic Queries, which also supports OVER
clause.
这篇关于如何在休眠nativeQuery中投射空日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!