所以我有一个NamedQuery,其中的字段是通过repo通过enityManager.createNamedQuery传递的。
所以我有两个字段,areaId和fieldId,areaId将始终存在,但fieldId有时为null。
如果:fieldId为空,如何省略(删除)以下行
and summary.bucket.fieldId.id = :fieldId --line to be removed
以下是我尝试使用案例方案的尝试,但这不起作用。
我愿意接受最好的方法或指导吗?
@NamedQuery(name = "SummaryBySubstatus.getInfo",
query = "select new com.model.group.summaryGroup(summary.bucket.area.id,"
summary.bucket.facilityProductInfo,
from SummaryBySubstatus as summary
where summary.bucket.area.id = :areaId
and summary.bucket.fieldId.id = :fieldId
---Tried This way---
and(case when :fieldId != null then summary.bucket.fieldId.id = :fieldId end)"
答案 0 :(得分:0)
and summary.bucket.fieldId.id = :fieldId
当fieldId
在兼容数据库(非MySQL)上为null时,它将始终为false。试试
where summary.bucket.area.id = :areaId
and ( :fieldId IS NOT NULL AND summary.bucket.fieldId.id = :fieldId
or :fieldId IS NULL AND summary.bucket.fieldId.id = :fieldId )
如果您使用的是PostgreSQL,则有一个错误需要通过调用来绕过
query.setParameter("fieldId", not_null_value_of_the_same_type);
query.setParameter("fieldId", the_real_value_that_can_be_null);
或者您可以只创建两个查询。