0

Context: I have three models, Owner, Property and Community. Property has a reference to its Owner, and another one to the Community.

I need to make the following query: find all the owners in a community which meet some criteria (floor number, property letter, etc, all the fields of the criteria are inside the Property class)

Is there any way to implement this in a repository without creating a bidirectional relationship or writing a native query? Something like:

Set<Owners> findAllByCommunityAndProperty_floorNumberAndProperty_letter(Community community, Property property);

(I would need a bidirectional relationship to make the query above)

Raul
  • 141
  • 7
  • You'll have to specify Property fields values separately, like `Set findAllByCommunityAndPropertyFloorNumberAndPropertyLetter(Community community, Integer floorNumber, Character letter);` [This](http://stackoverflow.com/questions/26684361/filter-child-object-in-spring-data-query) question may be related. – dfche Mar 24 '17 at 23:17

1 Answers1

0

You can use a query like this

SELECT o 
FROM Property p 
INNER JOIN property.owners o
WHERE p. ...

See http://www.objectdb.com/java/jpa/query/jpql/from#INNER_JOIN_ for various examples of join syntax.

In Spring Data JPA you will probably use the @Query annotation to bind that query to a method.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348