Hi guys!
I have many to many association.
user * - * product
Here is the code
Code:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
User u = (User) session.load(User.class,new Integer(2));
Product p = (Product) session.load(Product.class,new Integer(4));
p.getUserMany().add(u);
User u2 = new User();
u2.setName(u.getName());
u2.setPassword(u.getPassword());
session.save(u2);
Iterator it = u.getProductMany().iterator();
while(it .hasNext()) {
Product p1 = (Product) it.next();
p1.getUserMany().add(u2);
}
tx.commit();
session.close();
The query printed in the console is:
Code:
Hibernate: delete from user_product where product_id=?
Hibernate: insert into user_product (product_id, user_id) values (?, ?)
Hibernate: insert into user_product (product_id, user_id) values (?, ?)
Hibernate: insert into user_product (product_id, user_id) values (?, ?)
My question is: Why hibernate deletes the record from the side of the products instead of the side of the User and how to fix this?