0

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.

4

1 回答 1

0

抱歉,我发现问题/答案迟到了。

在尝试@EmbeddedId 之后,我不得不将@JoinColumn 可插入和可更新声明为false。

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;
}
于 2013-05-27T08:37:29.030 回答