Mapping a Many To Many Relationship on Single Key when Composite Key is defined

We have 2 entities Entity A and Entity B, where Entity A has a Composite Primary Key and these two entities have a many to many relationship. We need to define a JoinTable between these 2 such that the join table has only 2 columns and references only a part of the composite key for Entity A

Example:
Entity A

A_SEQ
TENANT_ID

Primary Key (A_SEQ, TENANT_ID)

Entity B

B_SEQ

Join Table (A_B_TABLE)

A_SEQ
B_SEQ

The join table should not have the Tenant Id column. Can we have this mapping in Hibernate?

Sure you can model this, just specify the join columns for the @JoinTable annotation instead of relying on the defaults. Note though, that you must create a unique constraint for A_SEQ by specifying e.g. @Column(unique = true), because a foreign key constraint can only reference columns that form a unique constraint.

@beikov I am getting this error if I am trying to map this:

Caused by: org.hibernate.AnnotationException: referencedColumnNames(A_SEQ) of org.hibernate.bugs.EntityB.entityBList referencing org.hibernate.bugs.EntityA not mapped to a single property

Example Code: GitHub - Akshit97/hibernate-test-case-templates at feature-many-to-many

EntityA.java

@Entity
@Access(AccessType.PROPERTY)
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class EntityA implements Serializable {

    private Long id1;
    private Long id2;
    private List<EntityB> entityBList;

    @Id
    @Column(name = "A_SEQ", unique = true)
    public Long getId1() {
        return id1;
    }

    @Id
    @Column(name = "A_SEQ_1")
    public Long getId2() {
        return id2;
    }

    @ManyToMany
    @JoinTable(name = "A_B_Table", joinColumns = @JoinColumn(name = "A_SEQ", referencedColumnName = "A_SEQ"),
    inverseJoinColumns = @JoinColumn(name = "B_SEQ", referencedColumnName = "B_SEQ"))
    public List<EntityB> getEntityBList() {
        return entityBList;
    }



}

EntityB.java

@Entity
@Access(AccessType.PROPERTY)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class EntityB {
    private Long id;

    @Id
    @Column(name = "B_SEQ")
    public Long getId() {
        return id;
    }
}

Thanks for the test case. Please create a Jira issue for this, attach the test case and comment a link to that Jira issue here for reference. We’ll try to look into this as fast as we can.

@beikov Sure, here is the JIRA
https://hibernate.atlassian.net/browse/HHH-17925

1 Like