Search code examples
javajpqlnamed

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

Solution

  • 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);