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

时间:2016-06-10 20:35:28

标签: java foreign-keys many-to-many composite-primary-key hibernate-annotations

我有3个数据表:
应用 {id_app,版本,名称}
客户 {org_id,name}
关联 {id_app,version,org_id,state}。

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
}


正确的注释是什么? Application和Customers之间的关系是多对多的,我为此创建了Association表,并为额外的列创建了#34; state&#34;。
现在我收到此错误:从sla.model.Association引用sla.model.Application的外键具有错误的列数。应该是2。
请帮忙。

1 个答案:

答案 0 :(得分:-1)

完成。我改变了以下内容:
在协会班:

@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")) 
})