与复合键的多对多关系,Hibernate 中的额外列

Many-to-many relationship with composite keys, extra column in Hibernate

我有3条数据table:
应用程序 {id_app, 版本, 名称}
客户 {org_id, 姓名}
协会 {id_app,版本,org_id,状态}。

Applications 有一个复合主键(id_app,version),Customers 的主键是 org_id,Associations 有一个复合主键(id_app,version,org_id)。

在我的 java 应用程序中,我有以下 类:

@Entity
@Table(name = "Applications")
@IdClass(ApplicationId.class)
public class Application implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID_APP", nullable = false)
    private String idApp;
    @Id
    @Column(name = "VERSION", nullable = false)
    private String version;
    @Column(name = "NAME")
    private String name;

    @OneToMany(mappedBy = "idPk.appPk", fetch = FetchType.LAZY) // cascade = CascadeType.ALL)
    private List<Association> custApps;

    // getters and setters
}


public class ApplicationId implements Serializable {

    private static final long serialVersionUID = 1L;

    private String idApp;
    private String version;

    //hashcode and equals
}


@Entity
@Table(name = "CUSTOMERS")
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ORG_ID", unique = true, nullable = false)
    private Integer orgID;

    @Column(name = "NAME")
    private String name;

    @OneToMany(mappedBy="idPk.customerPk", fetch = FetchType.LAZY)
    private List<Association> custApps;

    //getters and setters
}


@Entity
@Table(name = "ASSOCIATIONS")
@AssociationOverrides({
    @AssociationOverride(name = "idPk.appPk", joinColumns = @JoinColumn(name = "ID_APP")),
    @AssociationOverride(name = "idPk.appPk", joinColumns = @JoinColumn(name = "VERSION")),
    @AssociationOverride(name = "idPK.customerPk", joinColumns = @JoinColumn(name = "ORG_ID")) 
    })
public class Association implements Serializable {

    private static final long serialVersionUID = 1L;

    private AssociationId idPk = new AssociationId();
    private String state;

    public Association() {
        super();
    }

    @EmbeddedId
    public AssociationId getIdPk() {
        return idPk;
    }

    @Transient
    public Customer getCustomerPk() {
        return idPk.getCustomerPk();
    }

    @Transient
    public Application getAppPk() {
        return idPk.getAppPk();
    }

    @Column(name = "STATE")
    public String getState() {
        return state;
    }

    //setters , hashCode and equals
}


@Embeddable
public class AssociationId implements Serializable {

    private static final long serialVersionUID = 1L;

    private Application appPk;
    private Customer customerPk;

    // here is the problem ?
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumns({ @JoinColumn(name = "ID_APP", referencedColumnName = "ID_APP"),
        @JoinColumn(name = "VERSION", referencedColumnName = "VERSION") })
    public Application getAppPk() {
        return appPk;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="ORG_ID")
    public Customer getCustomerPk() {
        return customerPk;
    }

    //setter, hashCode and equals
}


正确的注释是什么?应用程序和客户之间的关系是多对多的,我为此和额外的列 "state" 创建了关联 table。
现在我收到此 错误 :从 sla.model.Association 引用 sla.model.Application 的外键列数错误。应该是 2 。
请帮忙。

完成。我更改以下内容:
在协会 class:

@AssociationOverrides({
@AssociationOverride(name = "idPk.appPk", joinColumns = { @JoinColumn(name = "ID_APP", referencedColumnName = "ID_APP"),
    @JoinColumn(name = "VERSION", referencedColumnName = "VERSION") }),
@AssociationOverride(name = "idPK.customerPk", joinColumns = @JoinColumn(name = "ORG_ID")) 
})