title: Spring JPARepository querying many to many intersection table
body: <p>I have 3 entity classes as follows (Example taken from <a href="https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/" rel="nofollow noreferrer">https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/</a>)</p>
  
  <p><strong>Book class</strong></p>
  
  <pre><code>@Entity
  public class Book{
      private int id;
      private String name;
      private Set&lt;BookPublisher&gt; bookPublishers;
  
      public Book() {
      }
  
      public Book(String name) {
          this.name = name;
          bookPublishers = new HashSet&lt;&gt;();
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
      public Set&lt;BookPublisher&gt;   getBookPublishers() {
          return bookPublishers;
      }
  
      public void setBookPublishers(Set&lt;BookPublisher&gt; bookPublishers) {
          this.bookPublishers = bookPublishers;
      }
  }
  </code></pre>
  
  <p><strong>Publisher class</strong></p>
  
  <pre><code>@Entity
  public class Publisher {
      private int id;
      private String name;
      private Set&lt;BookPublisher&gt; bookPublishers;
  
      public Publisher(){
  
      }
  
      public Publisher(String name){
          this.name = name;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(mappedBy = "publisher")
      public Set&lt;BookPublisher&gt; getBookPublishers() {
          return bookPublishers;
      }
  
      public void setBookPublishers(Set&lt;BookPublisher&gt; bookPublishers) {
          this.bookPublishers = bookPublishers;
      }
  }
  </code></pre>
  
  <p><strong>Intersection Table</strong></p>
  
  <pre><code>@Entity
  @Table(name = "book_publisher")
  public class BookPublisher implements Serializable{
      private Book book;
      private Publisher publisher;
      private Date publishedDate;
  
      @Id
      @ManyToOne
      @JoinColumn(name = "book_id")
      public Book getBook() {
          return book;
      }
  
      public void setBook(Book book) {
          this.book = book;
      }
  
      @Id
      @ManyToOne
      @JoinColumn(name = "publisher_id")
      public Publisher getPublisher() {
          return publisher;
      }
  
      public void setPublisher(Publisher publisher) {
          this.publisher = publisher;
      }
  
      @Column(name = "published_date")
      public Date getPublishedDate() {
          return publishedDate;
      }
  
      public void setPublishedDate(Date publishedDate) {
          this.publishedDate = publishedDate;
      }
  }
  </code></pre>
  
  <p>I want to query 2 things,</p>
  
  <ol>
  <li>Get list of books belonging to a particular publisher e.g. get all books associated with publisher 100</li>
  <li>Get list of books not associated with a particular publisher e.g. get all books not associated with publisher 100</li>
  </ol>
  
  <p>I want to achieve this using a simple JPARepository query if possible like findByXYZIn(...) etc.</p>
  
  <p>Please let me know if querying a many to many relation is possible using JPA repository queries and if yes, whether I can do it directly or would it require any changes in the entity classes</p>