CHAPTER 6 Mapping collections and entity associations and the row can be removed safely. You may have removed all other references manually. Or, if you didn t, the database constraints prevent any inconsistency, and you see a foreign key constraint exception. Hibernate offers you a way to declare this guarantee for collections of entity references. You can tell Hibernate, If I remove an element from this collection, it will be an entity reference, and it s going to be the only reference to that entity instance. You can safely delete it. The code that worked for deletion with a collection of components works with collections of entity references. This option is called cascade orphan delete. You can enable it on a collection mapping in XML as follows: … With annotations, this feature is available only as a Hibernate extension: public class Item { … @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, mappedBy = “item”) @org.hibernate.annotations.Cascade( value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN ) private Set bids = new HashSet(); … } Also note that this trick works only for collections of entity references in a one-tomany association; conceptually, no other entity association mapping supports it. You should ask yourself at this point, with so many cascading options set on your collection, whether a simple collection of components may be easier to handle. After all, you ve enabled a dependent lifecycle for objects referenced in this collection, so you may as well switch to the implicit and fully dependent lifecycle of components. Finally, let s look at the mapping in a JPA XML descriptor: …
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.
This entry was posted
on Sunday, January 27th, 2008 at 12:27 pm and is filed under PHP5.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.