@Entity public class Entity1 { @Id private long id; @ManyToMany private List<Entity2> entity2s; @ManyToMany private List<Entity2> otherEntity2s; ... // getter and setter. } @Entity public class Entity2 { @Id private long id; @ManyToMany(mappedBy = "entity2s") private List<Entity1> entity1s; @ManyToMany(mappedBy = "otherEntity2s") private List<Entity1> otherEntity1s; ... // getter and setter. }
This code generate this sql:
create table Entity1_Entity2 ( otherEntity1s_id bigint not null, otherEntity2s_id bigint not null, entity1s_id bigint not null, entity2s_id bigint not null )