-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Many to many problem
PostPosted: Sat Aug 26, 2006 11:30 am 
Newbie

Joined: Tue Mar 21, 2006 4:19 am
Posts: 15
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?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 26, 2006 11:26 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
Try setting a cascade on the User side, while leaving cascade="none" on the Product side. Also, make the Product side inverse="true". That should ensure that persistence of the relationship only goes from User -> Product.

_________________
-Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.