dnc253
dnc253

Reputation: 40337

Does Spring Data JPA have a naming convention for "member of"?

Spring Data JPA constructs a number of queries based on naming convention of the interface methods. Is there any naming convention to do a "MEMBER OF" query?

I can get the following to work with JPQL:

@Query("select e from MyEntity e where ?1 member of e.entities")

However, I'd like to use a name convention method if possible. I couldn't find anything in the docs about it, so I'm hoping I'm just missing something. If there's some fundamental reason there can't be methods for this, I'd be interested to know that too.

Upvotes: 5

Views: 14443

Answers (3)

Zomono
Zomono

Reputation: 852

The documentation seems to be incomplete about the Containing (IsContaining, Contains) key word.

https://dzone.com/refcardz/core-spring-data extends the documentation as follows:

CONTAINING: Containing, IsContaining, Contains (String or collection types only)

So you are able to use the Containing key word as a replacement for member of.

For example:

// select e from MyEntity e where ?1 member of e.entities
findByEntitiesContaining(TypeOfMember member);

Upvotes: 1

Marcus Bornman
Marcus Bornman

Reputation: 385

Unfortunately, the answer provided by Zerlono didn't work for me.

However, I achieved the desired effect through the use of findDistinctBy and by emitting Containing. Seems illogical, I know, but got the job done:

// select e from MyEntity e where ?1 member of e.entities
findDistinctByEntities(TypeOfMember member);

Upvotes: 1

Brandon Oakley
Brandon Oakley

Reputation: 310

Skimming the latest version of this document it doesn't look like there is. Here are the available method name keywords:

http://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#d0e1599

Upvotes: 2

Related Questions