JPA/Hibernate Native Queries do not recognize Parameters

The use of named parameters is not defined for native queries. From the JPA specification (section 3.6.3 Named Parameters):

Named parameters follow the rules for
identifiers defined in Section 4.4.1.
The use of named parameters applies to
the Java Persistence query language,
and is not defined for native queries.
Only positional parameter binding may
be portably used for native queries
.

So try the following instead:

String queryString = "select * from Cell c where ST_DWithin(c.shape, SetSRID(ST_GeomFromEWKT('POINT(?1 ?2)'),4326), 0.1)";
Query query = Cell.em().createNativeQuery(queryString, Cell.class);
query.setParameter(1, longitude);
query.setParameter(2, latitude);

Note that in JPA >= 2.0 you can use named parameters in native queries.

Leave a Comment