java 如何不选择空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3416130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to NOT select empty string
提问by Shervin Asgari
We have the following JPQL:
我们有以下 JPQL:
Select distinct sys.ipAddress from SystemLog sys where sys.ipAddress is not null and sys.ipAddress is not empty
And this generates the following mysqlstatement.
这将生成以下mysql语句。
select
distinct systemlog0_.ipAddress as col_0_0_
from
SystemLog systemlog0_
where
(
systemlog0_.ipAddress is not null
)
and (
exists (
select
systemlog0_.id
from
SystemLog systemlog0_
)
)
This obviously doesn't work and returns empty string instead of omitting it. However, I am looking for something like this to be generated:
这显然不起作用并返回空字符串而不是省略它。但是,我正在寻找生成这样的东西:
select distinct ipAddress from SystemLog where ipAddress is not null and ipAddress <> '';
However, I can't figure out why our jpa query doesn't generate something simliar like that. Any ideas?
但是,我不明白为什么我们的 jpa 查询不会生成类似的东西。有任何想法吗?
回答by Pascal Thivent
I think that you are misusing IS [NOT] EMPTYthat is used to check whether a collection association pathresolves to an empty collection or has at least one value. From the JPA specification:
我认为您正在滥用IS [NOT] EMPTY它用于检查集合关联路径是否解析为空集合或至少具有一个值。从 JPA 规范:
4.6.11 Empty Collection Comparison Expressions
The syntax for the use of the comparison operator IS EMPTYin an empty_collection_comparison_expressionis as follows:
collection_valued_path_expression IS [NOT] EMPTYThis expression tests whether or not the collection designated by the collection-valued path expression is empty (i.e, has no elements).
Example:
SELECT o FROM Order o WHERE o.lineItems IS EMPTYIf the value of the collection-valued path expression in an empty collection comparison expression is unknown, the value of the empty comparison expression is unknown.
4.6.11 空集合比较表达式
在 empty_collection_comparison_expression 中使用比较运算符IS EMPTY的语法 如下:
collection_valued_path_expression IS [NOT] EMPTY该表达式测试由集合值路径表达式指定的集合是否为空(即,没有元素)。
例子:
SELECT o FROM Order o WHERE o.lineItems IS EMPTY如果空集合比较表达式中的集合值路径表达式的值未知,则空比较表达式的值未知。
In my opinion, you should just use the <>comparison operator:
在我看来,您应该只使用<>比较运算符:
select distinct sys.ipAddress
from SystemLog sys
where sys.ipAddress is not null
and sys.ipAddress <> ''

