【发布时间】:2016-12-25 22:36:53
【问题描述】:
我有以下DAO方法:
public String getSomeTable(final String param1) {
String sqlString = "select * from table where name ilike ?";
Query query = this.getEntityManager().createNativeQuery(sqlString);
query.setParameter(1, "%param1%");
}
如果param1 是null 或为空,那么我想从表中选择所有条目。这样做的正确方法是什么?我目前正在使用以下内容:
public String getSomeTable(final String param1) {
String sqlString = "select * from table where name = ?";
Query query = this.getEntityManager().createNativeQuery(sqlString);
if(param1 == null)
query.setParameter(1, "%%");
else
query.setParameter(1, "%param1%");
}
但这不可扩展。我有整数、日期等数据类型。如果它是null,我想知道是否有办法跳过检查该参数。
我打算使用 COALESCE(?, CASE WHEN ? = '' THEN '%%' ELSE '%?%') 但我认为?对于特定参数,只能使用一次。下一个 > 我写的链接到第二个参数。
【问题讨论】:
-
该表是否允许空列值?如果是,是否需要将查询更改为:select * from table where name is null
-
您使用的是哪个数据库?
-
是的,该表允许 NULL 值。
-
我正在使用 Postgres
标签: java sql postgresql eclipselink nativequery