JPA蚀两个外键@IdClass实现错误 [英] JPA eclipse two foreign keys @IdClass implementation errors

查看:83
本文介绍了JPA蚀两个外键@IdClass实现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very new to Java EE and JPA/Eclipselink, so please bear with me. I'm trying to create a @Id for a table with no primary key and two composite keys, I tried to apply from a solution I read here by using @IdClass to mark the two foreign keys as a composite key, but I get this error when compiling and deploying the server.

异常描述:无效的复合主键规范.主键类[com.owl.server.objects.SaleDetails]中的主键字段或属性的名称与实体Bean类[com.owl.server.objects.SaleDetails]类的名称必须对应,并且其类型必须相同是相同的.此外,请确保已为XML中的相应属性指定了ID元素,并且/或者在实体类的相应字段或属性中指定了@Id.请参见server.log了解更多详细信息.

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.SaleDetails] 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.

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

Must I change the field types to be the same? Or is there another way?

产品表和相应的实体

@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 Table and Entity (Causes Errors)

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

Sale_details entity class

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

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

Here is the full stack trace error:

[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"的同一类

Idclass value should not be the same class of Entity '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;
        }
    }

这篇关于JPA蚀两个外键@IdClass实现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆