-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many to many problem
PostPosted: Tue Aug 22, 2006 1:04 pm 
Newbie

Joined: Tue Mar 21, 2006 4:19 am
Posts: 15
Hi guys!
I have association user - products many to many.
I have a user bean with collection of products. I want to create a new record of this user in the db but the hibernate does not connect the products to the new record here is the code:

Code:
tx = session.beginTransaction();
user.setIsHistory("1");
session.update(user);
tx.commit();

session.evict(user);

tx = session.beginTransaction();
user.setIsHistory("0");
session.save(user);
tx.commit();



Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 1:41 pm 
Newbie

Joined: Thu May 04, 2006 5:04 am
Posts: 17
Location: Austria
If its a new record then you should use saveOrUpdate() method I suppose.
edited: sorry, didnt see that you only have problems with the relation of the objects, not the save of them, forget this post.

_________________
Please don't forget to rate the posting if it helped you, thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 2:16 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you neet to set them in the object. I have no idea which is the parent or what, but you need to call something like

Code:
beginTransaction()
Child c = new Child();
c.setParent(parent)
Parent.addChild(c)
update(Parent)
commit()

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 3:07 am 
Newbie

Joined: Tue Mar 21, 2006 4:19 am
Posts: 15
Thx but i`m already doing this
i`ll paste the entire code here to see the whole situation:


this is my create method code:
Code:
tx = session.beginTransaction();
rtDTO.setIsHistory("1");
session.update(rtDTO);
tx.commit();
         
session.evict(rtDTO);
tx = session.beginTransaction();
rtDTO.setIsHistory("0");
// creates a new record in the database with rtDTO
session.save(rtDTO);
connectTraderToProducts(rtDTO);
tx.commit();


In this method when i invoke save() method the id is changed to the
last+1 in the table. But when i invoke the

here is the method "connectTraderToProducts" where i connect many to many association
Code:

List products = (List) rtDTO.getCommonExciseProductMany();
for (int i = 0; i < products.size(); i++) {
Product product = (Product ) products.get(i);
ArrayList existed = new ArrayList(product.getTraders());

/* ------

When i retrieve the traders for this product
the id of the Trader is rolled back to the previous one before
i evict() it from the session and invoke save() method
any idea why this happens?

*/ ---

boolean exist = false;
for (int j = 0; j < existed.size(); j++) {
   Trader rt = (Trader) existed.get(j);
      if (rt.getId().equals(rtDTO.getId())) {
          exist = true;
      }
   }
   if (!exist) {
       product.getTraders().add(rtDTO);
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.