简体   繁体   中英

Java Named Query

I'm trying to create a Java named query which does the following postgres query:

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 ' ')

@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.

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.

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

You also need to add AND and remove the comma after NULL.

Reference: Named Query with like in where clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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