I'm trying to make this query work in JPA:
SELECT * FROM contrat WHERE contrat_json @> '{"nom" :"hever"}';
It works perfectly with postgresql
but when I integrate it with JPA, I get the following error:
Parameter with that position [1] did not exist
My code:
@Transactional
@Query(nativeQuery = true,value = "select p from Contrat p where contrat_json @> '{\"nom\":\":nom\"}'")
public List<Contrat> findByNomRestrict(@Param("nom") String nom);
I think it does not recognize @>
despite native query, do you have an idea?
Parameter holders are not understood inside literals:
'...:nom...'
will contain the characters:nom
, not the bound values ofnom
.For PostgreSQL 9.5 (and later), use:
For 9.4:
For 9.3 (and earlier), there is no JSON containment operator (neither the
jsonb
type).http://rextester.com/AUHP11519
I had similar problem with my native query. The jsonb field name is called data, and it's simple
I want to find by name with JpaRepository, and here is my Repository
You need nativeQuery = true. With nativeQuery = true, this works as well.
I see your @Transactional annotation, I assume you have the native query on top of application service method. Can you try moving all native query's in repository and use JpaRepository, and use the repository method in your application service? Here is how my application service uses the repository.
Reference for JPA repository http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html
With PostgreSQL and JSON you'll probably run into needing
?
or other strange operators, so it's better you just use their function equivalents, instead. You can look them up in thepsql
console like this\doS+ @>
.Your query is not native, as the parameter says.
Will only give you an error when it reaches the database.
Try something like
and just bind
"{\"nom\":\"" + param + "\"}"
as the parameter