title: JPA - Retrieving records from various entities that share the same ForeignKey after an ID is passed in at the API
body: <p>Consider the following dummy data entities and its defined relationships:</p>
  
  <p><strong>Entity Business</strong></p>
  
  <pre><code>@Entity
  @Table(name = "business")
  public class Business {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  public Long id;
  
  @OneToMany(mappedBy = "Business")
  public List&lt;GiveMoney&gt; giveMoney;
  
  @OneToMany(mappedBy = "Business")
  public List&lt;RandomForm&gt; randomForm;
  
  @Column(name = "name")
  public String name;
  
  --  other fields &amp; getters and setters omitted --
  </code></pre>
  
  <p><strong>Entity GiveMoney</strong></p>
  
  <pre><code>@Entity
  @Table(name = "give_money")
  public class GiveMoney{
  
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  public Long id;
  
  @ManyToOne
  Business business;
  
  @OneToOne
  @JoinColumn(name = "random_form_id", insertable = false, updatable = false, 
  referencedColumnName = "id")
  public RandomForm randomForm;
  
  @OneToOne
  @JoinColumn(name = "person_giving_money_id", insertable = false, updatable = 
  false, 
  referencedColumnName = "id")
  public PersonGivingMoney personGivingMoney;
  
  @Column(name = "business_id", insertable = false, updatable = false)
  public Long business_id;
  
  @Column(name = "person_giving_money_id")
  public Integer person_giving_money_id;
  
  @Column(name = "random_form_id")
  public Integer  random_form_id;
  
  @Column(name = "how_much")
  public Integer howMuch;
  
  --  other fields &amp; getters and setters omitted --
  </code></pre>
  
  <p><strong>Entity RandomForm</strong></p>
  
  <pre><code>@Entity
  @Table(name = "random_form")
  public class RandomForm{
  
  @ManyToOne
  Business business
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  public Long id;
  
  @Column(name = "business_id", insertable = false, updatable = false)
  public Long business_id;
  
  --  other fields &amp; getters and setters omitted --
  </code></pre>
  
  <p>I've left out the final entity, personGivingMoney as it would only hold an Id which is referenced by person_giving_money_id and name.</p>
  
  <p>Please excuse the underscores, possibly incorrect insertable/updateable values and waste of space through unnecessary @Column annotations  </p>
  
  <p>Naturally, when I execute a getAll in my controller on the List GiveMoney It would retrieve a full list along with the joined columns from their respective entity. </p>
  
  <p>This is the findByBusiness_id in the <strong>GiveMoneyRepo</strong></p>
  
  <pre><code>    List&lt;GiveMoney&gt; findByBusiness_id(Long id);
  </code></pre>
  
  <p>and <strong>controller</strong></p>
  
  <pre><code>@RequestMapping(path = "/test/{id}")
  public List&lt;GiveMoney&gt;findAllWithGivenBusinessId(@PathVariable Long id){
      return giveMoneyRepo.findByBusiness_id(id);
   }
  </code></pre>
  
  <p>As expected, it would filter the GiveMoney list and return all records that hold the given business id at the api, <em>however</em> it would at the same time return random records from the RandomForm entity and PersonGivingMoney entity in the same result set. I believe this is normal because I've just specified my condition on the GiveMoney list through the GiveMoney repository, without telling Spring what to do with the rest of the entities/records.</p>
  
  <p>Both RandomForm and GiveMoney hold the business_id as a ForeignKey, I would like to give a business id in at the API and retrieve a list that would show the correct records from both entities based on the given id. Simply said:</p>
  
  <p>FindGiveMoneyByBusiness_Id &amp; FindRandomFormByBusiness_id into one JPA query &amp; resultset.</p>
  
  <p>I've read the documentation but clearly not well enough as Im sure Spring JPA has a way of doing this, alternatively I suppose I could be looking at creating a nativeQuery?</p>