title: Spring Data JPA Property Expressions for a many to many query
body: <p>I'm using Spring boot 2, with Spring data &amp; Spring Data JPA, and I'm looking to replace some existing <code>@Query</code> methods with Spring Data JPA's <a href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions" rel="nofollow noreferrer">Property Expressions</a></p>
  
  <p>However I'm struggling to workout the expression name for a many to many relationship. I'm not too sure if this is possible, as all the example I've seen are based on a one-to-many relationship.</p>
  
  <p>A basic example could be:</p>
  
  <p><strong>Book Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "book")
  public class Book {
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "title")
      private String title;
  
      @ManyToMany(mappedBy = "authors", cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
      private Set&lt;Author&gt; authors = new HashSet&lt;&gt;();
  
      // Getter and Setters
  }
  </code></pre>
  
  <p><strong>Author Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "Author")
  public class Author {
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "forename")
      private String firstName;
  
      @Column(name = "surname")
      private String surname;
  
      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      @JoinTable(
          name = "author_books",
          joinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
          uniqueConstraints = @UniqueConstraint(columnNames = {"author_id", "book_id"})
      )
      private Set&lt;Book&gt; books = new HashSet&lt;&gt;();
  
      // Getter and Setters
  }
  </code></pre>
  
  <p>The current SQL query looks something like this:</p>
  
  <pre><code>SELECT *
  FROM book
  LEFT JOIN author_books ON author_books.book_id = book.id
  WHERE author_books.author_id = 1;
  </code></pre>
  
  <p>How would you write the equivalent Spring JPA Property Expressions (if possible) within a Book repository?</p>
  
  <p>I've tried the following (with no luck):</p>
  
  <pre><code>List&lt;Book&gt; findByBooks_AuthorId(Long authorId);
  List&lt;Book&gt; findByBooks_Author_Id(Long authorId);
  List&lt;Book&gt; findByBooks_AuthorsId(Long authorId);
  List&lt;Book&gt; findByBooks_Authors_Id(Long authorId);
  </code></pre>
  
  <p>One solution would be to obtain the author object by it's id, and then calling <code>author.getBooks()</code>. However, I would like to be able to provide filtering and pagination of results using the spring data <code>PageRequest</code> and <code>Specification</code> classes.</p>
  
  <p>Any help would be really appreciated.</p>