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

时间:2022-05-26 11:28:29

I have 3 data table:
Applications {id_app, version, name}
Customers {org_id, name}
Associations {id_app, version, org_id, state}.

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).

我有3个数据表:Applications {id_app,version,name} Customers {org_id,name} Associations {id_app,version,org_id,state}。应用程序具有复合主键(id_app,version),Customers的主键是org_id,Associations具有复合主键(id_app,version,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. should be 2 .
Please help.

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

1 个解决方案

#1


-1  

Done. I change the following:
In Association class:

完成。我改变了以下内容:在Association类中:

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


#1


-1  

Done. I change the following:
In Association class:

完成。我改变了以下内容:在Association类中:

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