JPA "Friend" relationship between two objects of the same type

I have a specific JPA object that references other objects of the same type with the @ManyToMany relation.

@Entity(name = "GANG")
public class Gang {

  @Getter
  @Column(name = "NAME", nullable = false)
  private String name;

  @Getter
  @Column(name = "DESC", nullable = true)
  private String description;

  @ManyToMany
  private Set<Gang> allies;
}

Where the "allies" must point to a join table connecting the two groups. However, with my current understanding of JPA, when this object is created and a friend’s link is created, not all links are returned when called, since the Gang identifier (not displayed in my model, BINARY (16) UUID) may exist in both columns of the connection table .

+------------+------------+
|gang_a      |gang_b      |
+------------+------------+
|A           |B           |
+------------+------------+
|C           |A           |
+------------+------------+

When I get friends from gang A, I have to get gang B and gang C objects. How would I get in touch with this link?

+4
source share

All Articles