Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 外键';它也是JPA的主键_Java_Jpa_Foreign Keys_Primary Key - Fatal编程技术网

Java 外键';它也是JPA的主键

Java 外键';它也是JPA的主键,java,jpa,foreign-keys,primary-key,Java,Jpa,Foreign Keys,Primary Key,我有一个实体“Product”,它为另一个名为“Document”的类接收2个外键。这些外键也是Product类中的主键。我像这样构建代码,但是当我在eclipse中打开JPA内容图时,他说外键只是一个属性,而不是主键 文件: import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @Table(

我有一个实体“Product”,它为另一个名为“Document”的类接收2个外键。这些外键也是Product类中的主键。我像这样构建代码,但是当我在eclipse中打开JPA内容图时,他说外键只是一个属性,而不是主键

文件:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

@Table(name = "DOCUMENT")
@Entity
@IdClass(DocumentId.class)

public class Document {

    @Id
    private String typeDocument;
    @Id
    private String documentId;
    @Id
    private String version;
    @Id
    private String part;
    @Id
    private String fileId;
    @Id
    private String typeFile;
    @Id
    private String salesOrganizationId;
    private String productCenterSupplierId;
}
文档ID:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable

public class DocumentId implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "TYPE_DOCUMENT", length = 3)
    private String typeDocument;
    @Column(name = "DOCUMENT_ID", length = 25)
    private String documentId;
    @Column(name = "VERSION", length = 2)
    private String version;
    @Column(name = "PART", length = 3)
    private String part;
    @Column(name = "FILE_ID", length = 32)
    private String fileId;
    @Column(name = "TYPE_FILE", length = 3)
    private String typeFile;
    @Column(name = "SALES_ORGANIZATION_ID", length = 4)
    private String salesOrganizationId;
    @Column(name = "PRODUCT_CENTER_SUPPLIER_ID", length = 4)
    private String productCenterSupplierId;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((documentId == null) ? 0 : documentId.hashCode());
        result = prime * result + ((fileId == null) ? 0 : fileId.hashCode());
        result = prime * result + ((part == null) ? 0 : part.hashCode());
        result = prime * result + ((productCenterSupplierId == null) ? 0 : productCenterSupplierId.hashCode());
        result = prime * result + ((salesOrganizationId == null) ? 0 : salesOrganizationId.hashCode());
        result = prime * result + ((typeDocument == null) ? 0 : typeDocument.hashCode());
        result = prime * result + ((typeFile == null) ? 0 : typeFile.hashCode());
        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DocumentId other = (DocumentId) obj;
        if (documentId == null) {
            if (other.documentId != null)
                return false;
        } else if (!documentId.equals(other.documentId))
            return false;
        if (fileId == null) {
            if (other.fileId != null)
                return false;
        } else if (!fileId.equals(other.fileId))
            return false;
        if (part == null) {
            if (other.part != null)
                return false;
        } else if (!part.equals(other.part))
            return false;
        if (productCenterSupplierId == null) {
            if (other.productCenterSupplierId != null)
                return false;
        } else if (!productCenterSupplierId.equals(other.productCenterSupplierId))
            return false;
        if (salesOrganizationId == null) {
            if (other.salesOrganizationId != null)
                return false;
        } else if (!salesOrganizationId.equals(other.salesOrganizationId))
            return false;
        if (typeDocument == null) {
            if (other.typeDocument != null)
                return false;
        } else if (!typeDocument.equals(other.typeDocument))
            return false;
        if (typeFile == null) {
            if (other.typeFile != null)
                return false;
        } else if (!typeFile.equals(other.typeFile))
            return false;
        if (version == null) {
            if (other.version != null)
                return false;
        } else if (!version.equals(other.version))
            return false;
        return true;
    }

}
ProductId:

import java.io.Serializable;
import java.text.DecimalFormat;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.OneToOne;

@Embeddable

public class ProductId implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "PRODUCT_ID", length = 18)
    private String productId;
    @Column(name = "SALES_ORGANIZATION_ID", length = 4)
    private String salesOrganizationId;
    @Column(name = "BUSINESS_SECTOR", length = 2)
    private String businessSectorId;
    @Column(name = "SALES_OFFICE_ID", length = 4)
    private String salesOfficeId;
    @Column(name = "DISTRIBUTION_CHANNEL_ID", length = 2)
    private String distributionChannelId;
    @OneToOne
    private Document documentId;
    @OneToOne
    private Document Version;
    @Column(name = "PRODUCT_NAME", length = 40)
    private String productName;
    @Column(name = "VOLUME")
    private DecimalFormat volume;
    @Column(name = "HEIGHT")
    private DecimalFormat height;
    @Column(name = "WIDTH")
    private DecimalFormat width;
    @Column(name = "LENGTH")
    private DecimalFormat length;
    @Column(name = "WEIGHT_NET")
    private DecimalFormat weightNet;
    @Column(name = "WEIGHT_GROSS")
    private DecimalFormat weightGross;
    @Column(name = "NAME", length = 60)
    private String name;
    @Column(name = "HIERARCHY", length = 18)
    private String hierarchy;
    @Column(name = "CATEGORY_GROUP", length = 4)
    private String categoryGroup;
    @Column(name = "UNIT_MEASUREMENT", length = 3)
    private String unitMeasurement;
    @Column(name = "ORIGIN", length = 1)
    private String origin;
    @Column(name = "CLASSIFICATION_ID", length = 1)
    private String classificationId;
    @Column(name = "TEXT_COMPLEMENTAR", length = 80)
    private String textComplementar;
    @Column(name = "TAX_CLASSIFICATION", length = 16)
    private String taxClassification;
    @Column(name = "BAR_CODE_DUN14", length = 18)
    private String barCodeDun14;
    @Column(name = "BAR_CODE_EAN", length = 18)
    private String barCodeEan;
    @Column(name = "MULTIPLE")
    private DecimalFormat multiple;
    @Column(name = "CUBAGE")
    private DecimalFormat cubage;
    @Column(name = "CONTROL_CODE_TAX", length = 16)
    private String controlCodeTax;
    @Column(name = "CLASS", length = 20)
    private String classificationDesc;
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((Version == null) ? 0 : Version.hashCode());
        result = prime * result + ((barCodeDun14 == null) ? 0 : barCodeDun14.hashCode());
        result = prime * result + ((barCodeEan == null) ? 0 : barCodeEan.hashCode());
        result = prime * result + ((businessSectorId == null) ? 0 : businessSectorId.hashCode());
        result = prime * result + ((categoryGroup == null) ? 0 : categoryGroup.hashCode());
        result = prime * result + ((classificationDesc == null) ? 0 : classificationDesc.hashCode());
        result = prime * result + ((classificationId == null) ? 0 : classificationId.hashCode());
        result = prime * result + ((controlCodeTax == null) ? 0 : controlCodeTax.hashCode());
        result = prime * result + ((cubage == null) ? 0 : cubage.hashCode());
        result = prime * result + ((distributionChannelId == null) ? 0 : distributionChannelId.hashCode());
        result = prime * result + ((documentId == null) ? 0 : documentId.hashCode());
        result = prime * result + ((height == null) ? 0 : height.hashCode());
        result = prime * result + ((hierarchy == null) ? 0 : hierarchy.hashCode());
        result = prime * result + ((length == null) ? 0 : length.hashCode());
        result = prime * result + ((multiple == null) ? 0 : multiple.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((origin == null) ? 0 : origin.hashCode());
        result = prime * result + ((productId == null) ? 0 : productId.hashCode());
        result = prime * result + ((productName == null) ? 0 : productName.hashCode());
        result = prime * result + ((salesOfficeId == null) ? 0 : salesOfficeId.hashCode());
        result = prime * result + ((salesOrganizationId == null) ? 0 : salesOrganizationId.hashCode());
        result = prime * result + ((taxClassification == null) ? 0 : taxClassification.hashCode());
        result = prime * result + ((textComplementar == null) ? 0 : textComplementar.hashCode());
        result = prime * result + ((unitMeasurement == null) ? 0 : unitMeasurement.hashCode());
        result = prime * result + ((volume == null) ? 0 : volume.hashCode());
        result = prime * result + ((weightGross == null) ? 0 : weightGross.hashCode());
        result = prime * result + ((weightNet == null) ? 0 : weightNet.hashCode());
        result = prime * result + ((width == null) ? 0 : width.hashCode());
        return result;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProductId other = (ProductId) obj;
        if (Version == null) {
            if (other.Version != null)
                return false;
        } else if (!Version.equals(other.Version))
            return false;
        if (barCodeDun14 == null) {
            if (other.barCodeDun14 != null)
                return false;
        } else if (!barCodeDun14.equals(other.barCodeDun14))
            return false;
        if (barCodeEan == null) {
            if (other.barCodeEan != null)
                return false;
        } else if (!barCodeEan.equals(other.barCodeEan))
            return false;
        if (businessSectorId == null) {
            if (other.businessSectorId != null)
                return false;
        } else if (!businessSectorId.equals(other.businessSectorId))
            return false;
        if (categoryGroup == null) {
            if (other.categoryGroup != null)
                return false;
        } else if (!categoryGroup.equals(other.categoryGroup))
            return false;
        if (classificationDesc == null) {
            if (other.classificationDesc != null)
                return false;
        } else if (!classificationDesc.equals(other.classificationDesc))
            return false;
        if (classificationId == null) {
            if (other.classificationId != null)
                return false;
        } else if (!classificationId.equals(other.classificationId))
            return false;
        if (controlCodeTax == null) {
            if (other.controlCodeTax != null)
                return false;
        } else if (!controlCodeTax.equals(other.controlCodeTax))
            return false;
        if (cubage == null) {
            if (other.cubage != null)
                return false;
        } else if (!cubage.equals(other.cubage))
            return false;
        if (distributionChannelId == null) {
            if (other.distributionChannelId != null)
                return false;
        } else if (!distributionChannelId.equals(other.distributionChannelId))
            return false;
        if (documentId == null) {
            if (other.documentId != null)
                return false;
        } else if (!documentId.equals(other.documentId))
            return false;
        if (height == null) {
            if (other.height != null)
                return false;
        } else if (!height.equals(other.height))
            return false;
        if (hierarchy == null) {
            if (other.hierarchy != null)
                return false;
        } else if (!hierarchy.equals(other.hierarchy))
            return false;
        if (length == null) {
            if (other.length != null)
                return false;
        } else if (!length.equals(other.length))
            return false;
        if (multiple == null) {
            if (other.multiple != null)
                return false;
        } else if (!multiple.equals(other.multiple))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (origin == null) {
            if (other.origin != null)
                return false;
        } else if (!origin.equals(other.origin))
            return false;
        if (productId == null) {
            if (other.productId != null)
                return false;
        } else if (!productId.equals(other.productId))
            return false;
        if (productName == null) {
            if (other.productName != null)
                return false;
        } else if (!productName.equals(other.productName))
            return false;
        if (salesOfficeId == null) {
            if (other.salesOfficeId != null)
                return false;
        } else if (!salesOfficeId.equals(other.salesOfficeId))
            return false;
        if (salesOrganizationId == null) {
            if (other.salesOrganizationId != null)
                return false;
        } else if (!salesOrganizationId.equals(other.salesOrganizationId))
            return false;
        if (taxClassification == null) {
            if (other.taxClassification != null)
                return false;
        } else if (!taxClassification.equals(other.taxClassification))
            return false;
        if (textComplementar == null) {
            if (other.textComplementar != null)
                return false;
        } else if (!textComplementar.equals(other.textComplementar))
            return false;
        if (unitMeasurement == null) {
            if (other.unitMeasurement != null)
                return false;
        } else if (!unitMeasurement.equals(other.unitMeasurement))
            return false;
        if (volume == null) {
            if (other.volume != null)
                return false;
        } else if (!volume.equals(other.volume))
            return false;
        if (weightGross == null) {
            if (other.weightGross != null)
                return false;
        } else if (!weightGross.equals(other.weightGross))
            return false;
        if (weightNet == null) {
            if (other.weightNet != null)
                return false;
        } else if (!weightNet.equals(other.weightNet))
            return false;
        if (width == null) {
            if (other.width != null)
                return false;
        } else if (!width.equals(other.width))
            return false;
        return true;
    }
}

产品

import java.io.Serializable;
import java.text.DecimalFormat;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Table(name = "PRODUCT")
@IdClass(ProductId.class)
@Entity
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String productId;
    @Id
    private String salesOrganizationId;
    @Id
    private String businessSectorId;
    @Id
    private String salesOfficeId;
    @Id
    private String distributionChannelId;
    @OneToOne

    private Document documentId;

    @OneToOne
    private Document Version;
    private String productName;
    private DecimalFormat volume;
    private DecimalFormat height;
    private DecimalFormat width;
    private DecimalFormat length;
    private DecimalFormat weightNet;
    private DecimalFormat weightGross;
    private String name;
    private String hierarchy;
    private String categoryGroup;
    private String unitMeasurement;
    private String origin;
    private String classificationId;
    private String textComplementar;
    private String taxClassification;
    private String barCodeDun14;
    private String barCodeEan;
    private DecimalFormat multiple;
    private DecimalFormat cubage;
    private String controlCodeTax;
    private String classificationDesc;

    Gets and sets...

}

有人给我提建议吗?

如果我没记错的话,外键不是你自己构造的。当你在某物上添加一个
@OneToOne
@OneToMany
@ManyToOne
时,它们就会生成。备注:在一个POJO中有七个
@Id
-属性是可疑的…可能是重复的,它们会自动创建,但只是作为一个ATIBUTE,而不是teu其他类中的主键。我已经阅读了这个问题,并尝试在我的代码中实现答案。。。但是不起作用。