Let's say 2 entities are given: master and dependant.
They are defined in DB usually like dependants.master_id -> masters.id
, i.e. the dependant entity holds the reference to the main entity.
In JPA one2one BiDirectional association in this case usually looks like:
class Master {
@OneToOne(mappedBy="master")
Dependant dependant
}
class Dependant {
@OneToOne
@JoinColumn("master_id")
Master master
}
And this approach causes the necessity to deal with both sides of relation like:
Master master = new Master();
Dependant dependant = new Dependant();
dependant.setMaster(master);
master.setDependant(dependant);
repository.save(master);
instead of more intuitive and closer to business logic one like:
Master master = new Master();
Dependant dependant = new Dependant();
master.setDependant(dependant);
repository.save(master);
Are there any common workarounds for this? I mean I don't want to mess with supporting the association from the dependant side.