• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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);
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic