【问题标题】:How to prevent JPA query including a String constructor, throwing a NullPointerException when a null String value is only availabe如何防止包含字符串构造函数的 JPA 查询,当空字符串值仅可用时抛出 NullPointerException
【发布时间】:2016-08-09 02:12:49
【问题描述】:

我有以下 JPA 查询:select NEW java.lang.String(c.country) from Contact c group by c.country order by c.country

当联系人表中的某一行为国家/地区字段保存空值时,上述 JPA 查询中的字符串构造函数将引发 NullPointerException。

由于允许在联系人表中的国家/地区字段中使用空值,是否有任何方法可以通过代码阻止它并仍然使用命名查询?

【问题讨论】:

  • 在 WHERE 语句中使用 c.country != null 怎么样?

标签: java jpa constructor


【解决方案1】:

怎么样

select c.country from Contact c where c.country is not null group by c.country order by c.country

【讨论】:

    【解决方案2】:

    这里的 group by 没有任何意义,因为您在 select 子句中没有任何聚合函数。调用字符串构造函数也没用。

    随便用

    select c.country from Contact c order by c.country
    

    如果分组依据的目标是选择不同的值,请使用distinct

    select distinct c.country from Contact c order by c.country
    

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      相关资源
      最近更新 更多