简体   繁体   English

Java命名查询

[英]Java Named Query

I'm trying to create a Java named query which does the following postgres query: 我正在尝试创建一个Java命名查询,它执行以下postgres查询:

select * from call_attempt where result is NULL and id like '0000000101%';

The 0000000101 is set to id in my code, so this is what I want to do, but it's malformed: (I'm not sure how to set the id field while using the % and it has to be inside ' ') 0000000101在我的代码中被设置为id,所以这就是我想要做的,但它的格式不正确:(我不确定如何在使用%时设置id字段,它必须在''内)

@NamedQuery(name = AttemptEntity.JQL.NOTENDED,
                       query = "SELECT ae FROM AttemptEntity ae WHERE ae.result IS NULL,"
                             + " ae.correlationId LIKE '=:id%'")

First, you forgot the and , and replaced it by a comma. 首先,你忘记了and ,并用逗号替换它。

Second, the % must be passed as part of the argument: 其次, %必须作为参数的一部分传递:

SELECT ae FROM AttemptEntity ae WHERE ae.result IS NULL
and ae.correlationId LIKE :id

And then, when executing the query: 然后,在执行查询时:

String id = "0000000101%";
query.setParameter("id", id);

You can't have the % in the NamedQuery, but you can have it in the value you assign the parameter. 您不能在NamedQuery中拥有%,但您可以在分配参数的值中使用它。

query.setParamter("id", 0000000101+ "%");

You also need to add AND and remove the comma after NULL. 您还需要添加AND并在NULL后删除逗号。

Reference: Named Query with like in where clause 参考: 在where子句中使用like的命名查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM