This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cascade-save HELP !!!!

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • 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
  • 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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic