简体   繁体   English

JPA多对多关系:零或更多

[英]JPA many to many relationship : zero or more

I'm new to JPA so I have a question about a many to many relationship (with a zero to more implementation) : 我是JPA的新手,所以我有一个关于多对多关系(零到更多实现)的问题:

If you have a relationship like : 如果您有类似这样的关系:

在此处输入图片说明

Like stated a product can excist without a order (normally they will be added as new products arrive) later on it can be used on 1 or more orders. 如前所述,一种产品可以不经订单即存在(通常它们会随着新产品的到货而被添加),之后可以在一项或多项订单上使用。 An order must contain at least 1 or more products. 订单必须包含至少1个或更多产品。

you must state the relationship 你必须说明关系

@Entity(name = "ORDERS") 
public class Order {
       @Id 
       @Column(name = "ORDER_ID", nullable = false)
       @GeneratedValue(strategy = GenerationType.AUTO)
       private long orderId;

   @Column(name = "CUST_ID")
   private long custId;

   @Column(name = "TOTAL_PRICE", precision = 2)
   private double totPrice;     

   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(name="ORDER_DETAIL",
           joinColumns=
           @JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
     inverseJoinColumns=
           @JoinColumn(name="PROD_ID", referencedColumnName="PROD_ID")
   )
   private List<Product> productList;       
 ...............
   The other attributes and getters and setters goes here
}

and

@Entity(name = "PRODUCT") 
public class Product {
       @Id
       @Column(name = "PROD_ID", nullable = false)
       @GeneratedValue(strategy = GenerationType.AUTO)
       private long prodId;

   @Column(name = "PROD_NAME", nullable = false,length = 50)
   private String prodName;

   @Column(name = "PROD_DESC", length = 200)
   private String prodDescription;

   @Column(name = "REGULAR_PRICE", precision = 2)
   private String price;

   @Column(name = "LAST_UPDATED_TIME")
   private Date updatedTime;
   @ManyToMany(mappedBy="productList",fetch=FetchType.EAGER)
   private List<Order> orderList;       
   ...............
   The other attributes and getters and setters goes here
}

I wonder the zero to many relation is it still possible to just persist products who aren't linked (at the moment) to a order? 我想知道零对多关系是否仍然可以仅保留(目前)未链接到订单的产品?

But when a order uses a product the orderlist in product should be updated and the productlist in orde also. 但是,当订单使用产品时,应更新产品中的订单清单,也应更新orde中的产品清单。 How do I enforce this or does JPA this for me? 我该如何执行此操作,或者JPA可以为我执行此操作?

  1. You can still persist a product without order. 您仍然可以保留产品而无需订购。

  2. You have to update the other side of the relationship by hand. 您必须手动更新关系的另一端。 JPA won't do that for you. JPA不会为您这样做。 (of course if you save one order and then refetch the product your collection of order will be updated) (当然,如果您保存一个订单然后重新提取产品,则您的订单集合将被更新)

EDIT To explain second point: 编辑来解释第二点:

Product persitentProduct = ... //some product
Order newOrder = new Order();
newOrder.getProducts().add(persitentProduct);
//at this point : persistentProduct.getOrders().contains(newOrder)==false
entityManager.persist(newOrder);
//at this point nothing has changed on the other side of the relationship:
// i.e. : persistentProduct.getOrders().contains(newOrder)==false

You would then get something like : 然后,您将得到类似:

public class Order {
private List products;
...
public void addProduct(Product product) {
    this.products.add(product);
    if !(product.getOrders().contains(product) {
        product.getOrders().add(this);
    }
}
...
}



public class Product {
    private List orders;
    ...
    ...
}

right? 对? Or do I see this wrong 还是我看错了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM