JPA eclipse 两个外键@IdClass实现错误

JPA eclipse two foreign keys @IdClass implementation errors

我是 Java EE 和 JPA/Eclipselink 的新手,所以请多多包涵。我正在尝试为没有主键和两个复合键的 table 创建一个 @Id,我尝试通过使用 @IdClass 标记两个外键来应用我在此处阅读的解决方案键作为组合键,但在编译和部署服务器时出现此错误。

异常描述:复合主键规范无效。主键class[com.owl.server.objects.SaleDetails]中的主键字段或属性名称与实体beanclass[classcom.owl.server.objects.SaleDetails]必须对应并且它们的类型必须相同。此外,请确保您已为 XML and/or 中相应属性指定了 ID 元素,并在实体 class 的相应字段或属性上指定了 @Id。请参阅 server.log了解更多详情。

我必须将字段类型更改为相同吗?或者有别的办法吗?

产品table和相应的实体

@Entity
@Table(name="products")
public class Product implements Serializable {
    
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "product_id", nullable = false)
   private String product_id;
   
   // ...
}

销售额Table和相应的实体

@Entity
@Table(name="Sales")
public class Sale implements Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sale_id", nullable = false)
    private int sale_id;

    // ...
}

Sale_details Table 和实体(导致错误)

public class SaleDetails implements Serializable {

    private int saleid_fk;
    private String productid_fk;
    private int quantity_sold;
    private String prescription;
    
    public SaleDetails() {
    }
    
    public SaleDetails(int saleid_fk, String productid_fk, int quantity_sold){
        this.saleid_fk = saleid_fk;
        this.productid_fk = productid_fk;
        this.quantity_sold = quantity_sold;
    }
}

Sale_details实体class

@Entity
@IdClass(SaleDetails.class)
@Table(name = "sale_Details")
public class Sale_details {
    
   @Id
   private int saleid_fk;
    
   @Id
   private String productid_fk;
}

这是完整的堆栈跟踪错误:

[2020-12-24 11:43:50,553] Artifact server:war exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.7.7.payara-p3): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [owlJPA] failed.
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.7.7.payara-p3): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.owl.server.objects.SaleDetails] and those of the entity bean class [class com.owl.server.objects.Sale_details] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.. Please see server.log for more details.

Idclass 值不应与实体 'SaleDetails'

的 class 相同
    @Entity
    @IdClass(PK.class)
    public static class SystemUser{
        @Id
        private String subsystem;

        @Id
        private String username;

        public PK getId(){
            return new PK(subsystem, username);
        }

        public void setId(PK id){
            this.subsystem = id.subsystem;
            this.username = id.username;
        }
    }

    public static class PK implements Serializable {

        private String subsystem;

        private String username;

        public PK(String subsystem, String username) {
            this.subsystem = subsystem;
            this.username = username;
        }
    }

您可以使用“派生身份”;这是使用 @EmbeddedId:

映射关系的方法
@Embeddable
public class SaleDetailId {
    @Column(name = "product_id")
    String productId;  // corresponds to PK type of Product

    @Column(name = "sale_id")
    int saleId; // corresponds to PK type of Sale 
}


@Entity
@Table(name = "sale_details")
public class SaleDetail {
    @EmbeddedId
    SaleDetailId id;

    // id attribute mapped by join column default 
    @MapsId("saleId") // maps saleId attribute of embedded id 
    @ManyToOne
    private Sale sale;

    // id attribute mapped by join column default 
    @MapsId("productId") // maps productId attribute of embedded id 
    @ManyToOne
    private Product product;

    @Column(name = "quantity_sold")
    private int quantitySold;
    
    public SaleDetails(Sale sale, Product product, int quantity_sold){
        this.sale = sale;
        this.product = product;
        this.quantity_sold = quantity_sold;
    }
    // ...
}

使用 @IdClass 映射关系:

public class SaleDetailId {
    int sale; // matches name of @Id attribute and type of Sale PK 
    String product;  // matches name of @Id attribute and type of Product PK
}


@Entity
@IdClass(SaleDetailId.class)
@Table(name = "sale_details")
public class SaleDetail {

    // id attribute mapped by join column default 
    @Id
    @ManyToOne
    @JoinColumn(name = "sale_id")
    private Sale sale;

    // id attribute mapped by join column default 
    @Id
    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;

    @Column(name = "quantity_sold")
    private int quantitySold;
    
    public SaleDetails(Sale sale, Product product, int quantity_sold){
        this.sale = sale;
        this.product = product;
        this.quantity_sold = quantity_sold;
    }
    // ...
}

派生身份在第 2.4.1 节的 JPA 2.2 spec 中讨论(通过示例)。