title: Spring JPA Repository query filter by a relationship table
body: <p>If I have a many-to-many relationship between JPA entities as below, how can I retrieve a list of <code>Person</code> (I am interested in the person attributes) that are employees of a specific company?</p>
  
  <p>The relationship between <code>Person</code> and <code>Company</code> is many-to-many. The relationship table <code>Employee</code> has the FK to <code>Person</code> and <code>Company</code>, and a start_date and end_date to indicate when the employment started and finished.</p>
  
  <pre><code>@Entity
  public class Person {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(name = "name")
      private String name;
  
      @Column(name = "address")
      private String address;
  }
  
  @Entity
  public class Company {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(name = "name")
      private String name;
  
      @Column(name = "address")
      private String address;
  }
  
  @Entity
  public class CompanyEmployee {
      //note this is to model a relationship table. Am I doing this wrong?
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column(name = "start_date", nullable = false)
      private LocalDate startDate;
  
      @Column(name = "end_date", nullable = false)
      private LocalDate endDate;
  
      @ManyToOne
      private Company company;
  
      @ManyToOne
      private Person person;
  }
  </code></pre>
  
  <p>Do I use a <code>@Query</code> on the <code>CompanyEmployeeJPARepository</code>? How should I tackle it?</p>
  
  <pre><code>public interface CompanyEmployeeRepository extends JpaRepository&lt;CompanyEmployee,Long&gt; {
  //
  }
  </code></pre>