简体   繁体   English

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

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

I have 3 data table: 我有3个数据表:
Applications {id_app, version, name} 应用程序 {id_app,版本,名称}
Customers {org_id, name} 客户 {org_id,名称}
Associations {id_app, version, org_id, state}. 关联 {id_app,版本,org_id,州}。

Applications has a composite primary key (id_app, version), the primary key for Customers is org_id and Associations has a composite primary key (id_app, version, org_id). 应用程序具有复合主键(id_app,版本),客户的主键是org_id,关联具有复合主键(id_app,版本,org_id)。

In my java application I have the following classes: 在我的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
}


What are the correct annotation? 正确的注释是什么? The relationship is many to many between Application and Customers and I create the Association table for that and for the extra column "state". 应用程序和客户之间的关系是多对多的,我为此创建了关联表,并为额外的列“状态”创建了关联表。
Now I receive this error : A Foreign key refering sla.model.Application from sla.model.Association has the wrong number of column. 现在,我收到此错误 :从sla.model.Association引用sla.model.Application的外键具有错误的列数。 should be 2 . 应该是2。
Please help. 请帮忙。

Done. 做完了 I change the following: 我更改以下内容:
In Association 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")) 
})


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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