具有PK的JPA映射实体与具有复合PK的实体

时间:2016-02-02 20:20:26

标签: java jpa eclipselink

注意:我是JPA开发的新手,并且通过火力学习试用,所以请原谅我可能遗失的任何明显事实。

以下是相关的JPA实体。地址没有列出,但它是一个非常简单的实体,其中一个@Id命名为id。

ShipTo.java

    package model;

    import java.io.Serializable;
    import javax.persistence.*;
    import java.math.BigDecimal;
    import java.sql.Timestamp;


    /**
     * The persistent class for the ship_to database table.
     * 
     */
    @Entity
    @Table(name="ship_to")
    @NamedQuery(name="ShipTo.findAll", query="SELECT s FROM ShipTo s")
    public class ShipTo implements Serializable {
        private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ShipToPK id;

    //bi-directional many-to-one association to Customer
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="company_id", referencedColumnName="company_id"),
        @JoinColumn(name="customer_id", referencedColumnName="customer_id")
        })
    private Customer customer;

    //bi-directional one-to-one association to Address
    @OneToOne
    @JoinColumn(name="ship_to_id", referencedColumnName="id")
    private Address address;

ShipToPK.java

package model;

import java.io.Serializable;
import javax.persistence.*;

/**
 * The primary key class for the ship_to database table.
 * 
 */
@Embeddable
public class ShipToPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="company_id", insertable=false, updatable=false)
    private String companyId;

    @Column(name="ship_to_id")
    private long shipToId;

    public ShipToPK() {
    }
    public String getCompanyId() {
        return this.companyId;
    }
    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
    public long getShipToId() {
        return this.shipToId;
    }
    public void setShipToId(long shipToId) {
        this.shipToId = shipToId;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof ShipToPK)) {
            return false;
        }
        ShipToPK castOther = (ShipToPK)other;
        return 
            this.companyId.equals(castOther.companyId)
            && (this.shipToId == castOther.shipToId);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.companyId.hashCode();
        hash = hash * prime + ((int) (this.shipToId ^ (this.shipToId >>> 32)));

        return hash;
    }
}

运行应用程序时出现以下错误:

Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [ship_to.ship_to_id].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[address]
Descriptor: RelationalDescriptor(model.ShipTo --> [DatabaseTable(ship_to)])

我的ShipTo实体需要与基于ShipToPK的地址建立OneToOne关系.ship_to_id = Address.id

同样,我的Customer实体需要具有基于CustomerPK.customerId = Address.id的相同类型的关系

对此的任何帮助将不胜感激。谢谢,

1 个答案:

答案 0 :(得分:0)

您使用ship_to.ship_to_id字段有两个映射,两者都是可写的。通常,要使其工作,您需要确定要用于控制和设置该字段的映射,并将另一个映射为insertable = false,updatable = false。

在这种情况下,该字段是一个ID字段,也可以在您可能希望通过该关系设置的外键映射中使用。这是一个常见的特殊情况,JPA 2.0引入了派生ID支持,允许使用@ID注释标记关系。由于您使用的是embeddableID,因此您还可以使用@MapsId注释,以便JPA使用引用实体的值设置EmbeddedID的shipToId属性。

类似的东西:

//bi-directional one-to-one association to Address
@OneToOne
@MapsId("shipToId")
@JoinColumn(name="ship_to_id", referencedColumnName="id")
private Address address;

JoinColumn定义字段,而@MapsId强制它覆盖shipToId属性上的任何列定义。