Parameter as left hand operator LIKE JPQL

I'm trying the following query: (Using EclipseLink 2.5.2, MySQL 5.5)

em.createQuery("SELECT r FROM Region r WHERE (:pattern LIKE CONCAT(r.pattern, '%'))")
            .setParameter("pattern", "EU.FR.PR")
            .getResultList();

      

(Suppose I am trying to find all the superscope of a region. So the superscope EU.FR.PR

will be EU.FR

and EU

.)

However, this throws the following exception:

java.lang.IllegalArgumentException: you tried to set a value of type java.lang.String for a parameter template with the expected class type java.lang.Boolean from the query string

Oracle docs states that

The LIKE expression determines whether the pattern pattern matches a string.

The most important parts of my class Region

are:

@Entity
@NamedQuery(name="Region.findAll", query="SELECT r FROM Region r")
public class Region implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String pattern;

    public Region() {
    }

    public String getPattern() {
        return this.pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

}

      

Here are some examples of data from the table:

+----+-------------+
| id | pattern     |
+----+-------------+
|  3 | EU          |
|  5 | EU.FR       |
|  6 | EU.FR.AL    |
| 13 | EU.FR.LU    |
| 14 | EU.FR.PR    |
| 15 | EU.FR.PR.BA |
| 16 | EU.FR.PR.CA |
| 17 | EU.FR.PR.PA |
| 18 | EU.FR.RH    |
| 19 | EU.FR.RH.TV |
| 20 | EU.FR.RO    |
+----+-------------+

      

So why should I go through a boolean?


Direct concatenation of the scope into query ( "SELECT r FROM Region r WHERE ('" + theRegion + "' LIKE CONCAT(r.pattern, '%'))"

) gives the expected results, but that's not what I want to do in production. Is this a bug in EclipseLink?

+3


source to share





All Articles