javahibernatemany-to-manyhibernate-mappingpersistent

Relationship table data overwritten in many-to-many hibernate


I have two entity classes Order and Product with many-to-many relationship (several orders may have the same product as far as stock is not empty and an order may contain multiple products)as follows:

@Entity
@Table(name = "Order_")
public class Order implements Serializable {

    .......
@ManyToMany(mappedBy="orders")
private List<Product> products;  
    .......
}

@Entity
@Table(name = "Product")
public class Product implements Serializable {

.......
@ManyToMany
@JoinTable(name = "product_order", 
        joinColumns = { @JoinColumn(referencedColumnName="id") }, 
        inverseJoinColumns = { @JoinColumn(referencedColumnName="id") })
private List <Order> orders;
.......
}

Following is the Dao that is used to insert order and product. The products are predefined and I only need to update their quantity. The order is to be inserted as new order. The product_order relationship table has to be generated and data inserted as primary keys from order and product.

public class OrderDao {

 public static void insertProductOrder(Order order, List <Product> cart, List<Integer> quantity){

    ...............
    session.beginTransaction();

    Query query;

    Product pr;

    List list;

    List <Order> orders = new ArrayList<>();
    orders.add(order);

    for(int i=0;i<cart.size();i++) {

    query = session.createQuery("from Product where id = :pid");
    query.setParameter("pid", cart.get(i).getId());
    list = query.list();
    pr = (Product) list.get(0);

    pr.setQuantity(cart.get(i).getQuantity() - quantity.get(i));

    cart.get(i).setOrders(orders);
    }

    order.setProducts(cart);

    session.save(order);

    session.getTransaction().commit();
 }
}'

With this scenario, data is properly inserted into all three tables. But when I try to insert a new order, the data in the relationship table i.e. product_order is overwritten. The record which is overwritten is the one being updated for quantity. This is due to the fact that Hibernate is removing the record from product_order and then inserting it again. What I want is no updation of product_order table as it is not needed. I just want to insert a record into order table, update quantity in the product table and insert order_id and product_id into the product_order table. Thank you in advance.


Solution

  • Got it working by properly identifying the owner entity and making changes as follows:

    @Entity
    @Table(name = "Order_")
    public class Order implements Serializable {
    ....... 
    @ManyToMany
    @Cascade({CascadeType.PERSIST})
    @JoinTable(name = "product_order", 
            joinColumns = { @JoinColumn(referencedColumnName="id") }, 
            inverseJoinColumns = { @JoinColumn(referencedColumnName="id") })
    private List<Product> products;
    .......
    }
    
    
    @Entity
    @Table(name = "Product")
    public class Product implements Serializable {
    .......
    @Cascade({CascadeType.PERSIST})
    @ManyToMany(mappedBy="products")
    private List <Order> orders;
    .......
    }