简体   繁体   中英

Creating Join table with multiple Primary Keys?

I have a problem creating a Join Table with multiple primary keys. Below is my table structure:

表结构

Below is my JPA implementation:

@Entity
@Table(name = "label")
public class Label {
    @Id
    @Column(name = "tenant_id")
    private String tenant_id;

    @Id
    @Column(name = "label_id")
    private String label_id;

    @JsonIgnore
    @ManyToMany(targetEntity = Report.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "label_id"), inverseJoinColumns = @JoinColumn(name = "report_id"))
    private Set<Report> reports;
}

@Entity
@Table(name = "report")
public class Report {
    @Id
    @Column(name = "tenant_id")
    private String tenant_id;

    @Id
    @Column(name = "report_id")
    private String report_id;

    @column(name = "created_by")
    private String created_by;

    @JsonIgnore
    @ManyToMany(targetEntity = Label.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "label_id"))
    private Set<Label> labels;

}

This works fine, but when I try to add in tenant_id into the mix like so:

Label.java edit:

@JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "label_id"), inverseJoinColumns = { @JoinColumn(name = "report_id"), @JoinColumn(name = "tenant_id") })
private Set<Report> reports;

Report.java edit:

@JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = { @JoinColumn(name = "label_id"), @JoinColumn(name = "tenant_id") })
private Set<Label> labels;

I get an error saying that “A Foreign key refering has the wrong number of column. should be 1” “A Foreign key refering has the wrong number of column. should be 1” . As far as I understand, this is because I only have one primary key named Report and label each so I cannot map the tenant_id as a JoinColumn . However, I was wondering, is there any way to set the tenant_id when the report_id and label_id are set also? Any help would be appreciated. Thanks!

EDIT: The @Id fields in the entities are tenant_id and id in both the Report and Label classes.

Sorry, I'm not able to try this out but from the top of my head, it's something like this. There are a few things to fix like the complete annotation properties as the column names and class members are not the same but that is the idea. :)

more info here

Report class

@Entity
public class Report {

    @Id
    private Integer id;

    @Column
    private String createdBy;

    @Column
    private String tenantId;

    @OneToMany
    private List<Tagging> taggings = new ArrayList<>();

    // getter and setters

}

Label class

@Entity
public class Label {

    @Id
    private Integer id;

    @Column
    private String tenantId;

    @OneToMany
    private List<Tagging> taggings = new ArrayList<>();

    // getter and setters

}

Tagging class

@Entity
public class Tagging {

    @EmbeddedId
    private TaggingId id;

    @OneToMany
    private Report report;

    @OneToMany
    private Label label;

    // getter and setters

}

@Embeddable
class TaggingId {

    private Integer reportId;

    private String labelId;

    private String tenantId;

    // getter and setters

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM