简体   繁体   中英

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) :

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.

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. How do I enforce this or does JPA this for me?

  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. (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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