• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Cascade-save HELP !!!!

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 entities viz. Item and Bid. I have a bidirectional association.
i.e.
one-to-many association From Item to Bid.
many-to-one association from Bid to Item.

The following is the part of Item.hbm.xml :

<!-- Item - Bid Association - One-to-Many -->
<set name="bids" inverse="true" cascade="save-update">
<key column="ITEM_ID" ></key>
<one-to-many class="Bid"/>
</set>

Now when i say session.save(item) -> it works...
but when i add aBid to item using item.addBid(aBid); //addBid is just a
//convenient method
this is not making aBid persistent. (not inserting a row in BID table)


 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got my mistake.
Actually i was doing

session1.save(item1);
session1.close(); // NOTE : After this stmt, item1 will be detatched

Session session2 = .....;
item1.add(bid1); // Note : item1 is detached object
//cascade-save works only for persistent object and not
//detached objects
session2.getTransaction().commit();
session2.close();

Solution 1 : I should have added bid1 to item1 before session1.close();
as item1 is persistent before session1.close();
Solution 2 : I should have loaded item1 using
Item item2 = (Item)session2.get(Item.class,item.getItemId());
//Note now item2 is persistent (attached to a session)
item2.add(bid1);
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic