非主要列上的JPA映射单向

时间:2017-09-27 12:43:58

标签: java hibernate jpa

我有2个表Customer和CustomerAgreement。

客户结构:

primary key : id, validFrom

|----------------------------------|
|id | validFrom | custName | region|
|---|-----------|----------|-------|
| 1 | 54353     | Volvo    | APAC  |
| 1 | 54453     | Merc     | APAC  |
|----------------------------------|

CustomerAgreement的结构

 primary key : id, validFrom

|-----------------------------------------|
|id | validFrom | custId | name           |
|---|-----------|--------|--------------  |
|20 | 1867      |   2    | Merc_EOD_AGR   |
|21 | 8535      |   2    | Merc_SOD_AGR   |
|25 | 8335      |   1    | Volvo_SOD_AGR  |
|26 | 8635      |   1    | Volvo_SOD_AGR  |
|-----------------------------------------|

客户与客户协议之间的关系为1-N。 客户必须是拥有者,即如果我取得客户,我应该能够获得所有的客户服务。

以下是JPA类实现

@Entity
@Table(name = "Customer"
        , schema = "dbo"
)
public class Customer implements java.io.Serializable {


    private CustomerId id;
    private String custName;
    private Set<CustomerAgreement> customerAgreement;

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
            @AttributeOverride(name = "validFrom", column = @Column(name = "validFrom", nullable = false))})
    public CustomerId getId() {
        return this.id;
    }

    public void setId(CustomerId id) {
        this.id = id;
    }

    @Column(name = "custName", nullable = false, length = 64)
    public String getCustName() {
        return this.custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL,targetEntity = CustomerAgreement.class, mappedBy = "customerId")
    public Set<CustomerAgreement> getCustomerAgreement() {
        return customerAgreement;
    }

    public void setCustomerAgreement(Set<CustomerAgreement> customerAgreement) {
        this.customerAgreement = customerAgreement;
    }

}

@Embeddable
public class CustomerId implements java.io.Serializable {


    private int id;
    private int validFrom;

    @Column(name = "id", nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }


    @Column(name = "validFrom", nullable = false)
    public int getValidFrom() {
        return this.validFrom;
    }

    public void setValidFrom(int validFrom) {
        this.validFrom = validFrom;
    }


    public boolean equals(Object other) {
        if ((this == other)) return true;
        if ((other == null)) return false;
        if (!(other instanceof CustomerId)) return false;
        CustomerId castOther = (CustomerId) other;

        return (this.getId() == castOther.getId())
                && (this.getValidFrom() == castOther.getValidFrom());
    }

    public int hashCode() {
        int result = 17;

        result = 37 * result + this.getId();
        result = 37 * result + this.getValidFrom();
        return result;
    }
}

@Entity
@Table(name = "CustomerAgreement"
        , schema = "dbo"
)
public class CustomerAgreement implements java.io.Serializable {

    private CustomerAgreementId customerAgreementId;
    private String name;
    private int customerId;

    @EmbeddedId
    public CustomerAgreementId getCustomerAgreementId() {
        return this.customerAgreementId;
    }

    public void setCustomerAgreementId(CustomerAgreementId customerAgreementId) {
        this.customerAgreementId = customerAgreementId;
    }

    @Column(name = "name", nullable = false, length = 100)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "customerId", nullable = false)
    public int getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
}


@Embeddable
public class CustomerAgreementId  implements java.io.Serializable {


     private int id;
     private int validFrom;


    @Column(name="id", insertable=false, updatable=false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }


    @Column(name="validFrom", insertable=false, updatable=false)
    public int getValidFrom() {
        return this.validFrom;
    }

    public void setValidFrom(int validFrom) {
        this.validFrom = validFrom;
    }


   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
         if ( (other == null ) ) return false;
         if ( !(other instanceof CustomerAgreementId) ) return false;
         CustomerAgreementId castOther = ( CustomerAgreementId ) other; 

         return (this.getId()==castOther.getId())
 && (this.getValidFrom()==castOther.getValidFrom());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + this.getId();
         result = 37 * result + this.getValidFrom();
         return result;
   }   


}

我想基于customerId获取Customer和CustomerAgreements。 Customer和CustomerAgreement之间的映射位于Customer表中的id列和CustomerAgreement表中的customerId。

当我尝试获取时,我得到了错误

Foreign key (FKfclni2iqr01tuwaclmd7mtbpv:CustomerAgreement [customerId])) must have same number of columns as the referenced primary key (Customer [id,validFrom])

我尝试将CustomerAgreement类中的customerId的int数据类型替换为Customer,如下所示

@ManyToOne
    @JoinColumn(referencedColumnName = "id")
    public Customer getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Customer customerId) {
        this.customerId = customerId;
    }

仍然无法获得相同的错误

0 个答案:

没有答案