Search code examples
javajpaeclipselink

JPA (Eclipselink) @OneToMany misses some entrys


I got 2 Entities

Customer.class

@Entity
@Table(name = "Customer")
public class Customer {
    @Id
    private int id;
    @OneToMany(mappedBy = "customer"; fetch = FetchType.EAGER)
    private Set<Invoice> invoice;
}

and

Invoice.class

@Entity
@Table(name = "Invoice")
public class Invoice {
    @Id
    @ManyToOne
    @JoinColumn(name = "customer")
    private Customer customer;
    @Column(name = "price", nullable = false)
    private double price;
}

They are both mapped in the persistence.xml.

So:

System.out.println(customer);

for a specific customer gives me 30 Invoice entrys, but I got 33 in the Database.

I use org.eclipse.persistence.jpa 2.5.0 and persistence-api 1.0.2

I appreciate every hint/solution.

Thanks in advance.


Solution

  • Sorry for the late reply I found the problem/answer.

    After just trying @EmbeddedId I had to declare the @JoinColumn insertable and updatable false.

    The right mapping for the Invoice.class

    @Entity
    @Table(name = "Invoice")
    public class Invoice {
        @EmbeddedId
        private InvoiceId invoiceId;
        @ManyToOne
        @JoinColumn(name = "customerId", insertable = false, updatable = false)
        private Customer customer;
        @Column(name = "price", nullable = false)
        private double price;
    }
    
    @Embeddable
    class InvoiceId implements Serializable {
        //Composite PK
        @Column(name = "customerId")
        private int customerId;
        @Column(name = "date")
        private Date date;
    }