Consider the following dummy data entities and its defined relationships:
Entity Business
@Entity
@Table(name = "business")
public class Business {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Long id;
@OneToMany(mappedBy = "Business")
public List<GiveMoney> giveMoney;
@OneToMany(mappedBy = "Business")
public List<RandomForm> randomForm;
@Column(name = "name")
public String name;
-- other fields & getters and setters omitted --
Entity GiveMoney
@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 & getters and setters omitted --
Entity RandomForm
@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 & getters and setters omitted --
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.
Please excuse the underscores, possibly incorrect insertable/updateable values and waste of space through unnecessary @Column annotations
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.
This is the findByBusiness_id in the GiveMoneyRepo
List<GiveMoney> findByBusiness_id(Long id);
and controller
@RequestMapping(path = "/test/{id}")
public List<GiveMoney>findAllWithGivenBusinessId(@PathVariable Long id){
return giveMoneyRepo.findByBusiness_id(id);
}
As expected, it would filter the GiveMoney list and return all records that hold the given business id at the api, however 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.
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:
FindGiveMoneyByBusiness_Id & FindRandomFormByBusiness_id into one JPA query & resultset.
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?