Java 如何不选择空字符串
我们有以下JPQL:Java 如何不选择空字符串,java,mysql,jpa,orm,Java,Mysql,Jpa,Orm,我们有以下JPQL: Select distinct sys.ipAddress from SystemLog sys where sys.ipAddress is not null and sys.ipAddress is not empty 这将生成以下mysql语句 select distinct systemlog0_.ipAddress as col_0_0_ from SystemLog systemlog0_ where ( syste
Select distinct sys.ipAddress from SystemLog sys where sys.ipAddress is not null and sys.ipAddress is not empty
这将生成以下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_
)
)
这显然不起作用,并返回空字符串,而不是忽略它。
但是,我希望生成类似这样的内容:
select distinct ipAddress from SystemLog where ipAddress is not null and ipAddress <> '';
从系统日志中选择不同的ipAddress,其中ipAddress不为null且ipAddress为“”;
然而,我不明白为什么我们的jpa查询没有生成类似的东西。
有什么想法吗 我认为您误用了
IS[NOT]EMPTY
,用于检查集合关联路径是否解析为空集合或至少有一个值。根据JPA规范:
4.6.11空集合比较表达式
使用的语法
比较运算符在
空\u集合\u比较\u表达式
详情如下:
collection_valued_path_expression IS [NOT] EMPTY
这个表达式测试是否
指定的集合
集合值路径表达式为
空(即,没有元素)
例如:
SELECT o
FROM Order o
WHERE o.lineItems IS EMPTY
如果集合的值为
空集合中的路径表达式
比较表达式未知,则
空比较的值
表达式未知
在我看来,您应该只使用
比较运算符:
select distinct sys.ipAddress
from SystemLog sys
where sys.ipAddress is not null
and sys.ipAddress <> ''
选择不同的sys.ipAddress
从系统日志系统
其中sys.ipAddress不为空
和sys.ipAddress“”
“不为空”似乎无法检测空白字符串。我正在使用hibernate和JPA。jpql的操作符是否正常?@Amalgovinus是,
(和!=
)检查jpql中的字符串是否为空。看这里