首页 > 解决方案 > JPA eclipse 两个外键@IdClass 实现错误

问题描述

我对 Java EE 和 JPA/Eclipselink 很陌生,所以请多多包涵。我正在尝试为@Id没有主键和两个复合键的表创建一个,我尝试通过使用@IdClass将两个外键标记为复合键来从我在这里阅读的解决方案中应用,但是在编译和部署服务器。

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

我必须将字段类型更改为相同吗?还是有其他方法?

产品表和对应的实体

产品表

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

销售表及对应实体

销售表

@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 表和实体(导致错误)

Sale_details 表

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 实体类

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

标签: javamysqlhibernatejpaeclipselink

解决方案


Idclass 值不应与实体“SaleDetails”的同一类

    @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;
        }
    }

推荐阅读