Mapping a parent/children relationship
… The separate mapping for Bid is no longer needed. If you really want to make this a one-to-many entity association, Hibernate offers another convenience option you may be interested in. Enabling orphan deletion The cascading option we explain now is somewhat difficult to understand. If you followed the discussion in the previous section, you should be prepared. Imagine you want to delete a Bid from the database. Note that you aren t deleting the parent (the Item) in this case. The goal is to remove a row in the BID table. Look at this code: anItem.getBids().remove(aBid); If the collection has the Bid mapped as a collection of components, as in the previous section, this code triggers several operations: The aBid instance is removed from the collection Item.bids. Because Bid is mapped as a value type, and no other object can hold a reference to the aBid instance, the row representing this bid is deleted from the BID table by Hibernate. In other words, Hibernate assumes that aBid is an orphan if it s removed from its owning entity s collection. No other in-memory persistent object is holding a reference to it. No foreign key value that references this row can be present in the database. Obviously, you designed your object model and mapping this way by making the Bid class an embeddable component. However, what if Bid is mapped as an entity and the collection is a ? The code changes to anItem.getBids().remove(aBid); session.delete(aBid); The aBid instance has its own lifecycle, so it can exist outside of the collection. By deleting it manually, you guarantee that nobody else will hold a reference to it,
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.