Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneToOne Double Bidirectional Association

I am newbie to UML diagrams and would like to code the following one, where there is an OneToOne double bidirectional association, with JPA annotations.

Double OneToOne association

Context: there are persons and teams. Each team is composed of persons and each person can only belong to one team. The teams have always a person acting as main Leader and optionally can have a second Leader.

Based on this page, I have written the code below but I am not sure if it is right.

@Entity
public class Person extends BaseEntity {

...

    @NotNull
    @OneToOne(mappedBy="mainLeader", cascade = CascadeType.ALL, orphanRemoval = true)
    private Team mainTeam;

    @NotNull
    @OneToOne(mappedBy="secondLeader", cascade = CascadeType.ALL, orphanRemoval = true)
    private Team secondTeam;    
}

@Entity
public class Team extends BaseEntity {

...

    @NotNull
    @OneToOne
    private Person mainLeader;

    @OneToOne
    private Person secondLeader;    
}
like image 296
julianfperez Avatar asked Mar 14 '26 01:03

julianfperez


1 Answers

The association names suggests roles to be used. Main Leader might correspond to a role mainLeader. So I would just create a property in Team.

But since the associations are just named and do not have roles you are open to create roles at wish. This should be derived from the context.

like image 190
qwerty_so Avatar answered Mar 16 '26 15:03

qwerty_so



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!