javajpaeclipselinkjpa-2.1java-ee-8

JPA eclipselink persist entity that has no primary key but two foreign keys


I am very new to java EE and self-learning it, so please be patient with me. So far I have 3 simple tables in my database:

Sales Products
Sale_id Product_id
Sale_details
Saleid_fk
Productid_fk (fk)
Quantity_sold

How do I persist a new sale_details without creating a sale_details entity. Because Java wont let me name sale_details as @Entity without a primary key. this current code gives me an error stating i need to declare a primary key. I tried using Idclass based on the answers on stackoverflow but it doesn't do anything.

@Entity
@Table(name="sale_details")
@IdClass(value = SaleDetails.class)
public class SaleDetails implements Serializable {

    @Column(name = "saleid_fk")
    private int saleid_fk;

    @Column(name = "productid_fk")
    private String productid_fk;

    @Column(name = "quantity_sold")
    private int quantity_sold;

what am I missing here?
sale_details table:


Solution

  • You can just set a primary id as autoincrement and not use it.