EclipseLink - bidirectional OneToMany relation

后端 未结 2 1886
悲&欢浪女
悲&欢浪女 2021-01-15 05:39

Let\'s say I have two entities:

@Entity
public class Customer implements Serializable {
    ...
    @OneToMany(cascade=ALL, mappedBy=\"customer\")
    public         


        
2条回答
  •  死守一世寂寞
    2021-01-15 06:06

    If you retrieve the customer from the same transaction, then yes, it's expected behavior. The reason is that the EntityManager returns the order it has in its first-level cache, and that you created yourself, without adding any order to its set of orders. It's your responsibility to maintain the coherence of the object graph by maintaining the two sides of the association:

    order.setCustomer(customer);
    customer.addOrder(order);
    

提交回复
热议问题