Search code examples
javamysqlhibernatejpaeclipselink

JPA eclipse two foreign keys @IdClass implementation errors


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.

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?

Products table and corresponding Entity

Products 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;
   
   // ...
}

Sales Table and corresponding Entity

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

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

Solution

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