Web server info - CHAPTER 12 Modifying objects efficiently In certain situations,

CHAPTER 12 Modifying objects efficiently In certain situations, you want to delete an entity instance by removing a reference from a collection. In other words, you can guarantee that once you remove the reference to this entity from the collection, no other reference will exist. Therefore, Hibernate can delete the entity safely after you ve removed that single last reference. Hibernate assumes that an orphaned entity with no references should be deleted. In the example domain model, you enable this special cascading style for the collection (it s only available for collections) of bids, in the mapping of Item: You can now delete Bid objects by removing them from this collection for example, in detached state: Item anItem = … // Loaded in previous Session anItem.getBids().remove(aBid); anItem.getBids().remove(anotherBid); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.saveOrUpdate(anItem); tx.commit(); session.close(); If you don t enable the delete-orphan option, you have to explicitly delete the Bid instances after removing the last reference to them from the collection: Item anItem = … // Loaded in previous Session anItem.getBids().remove(aBid); anItem.getBids().remove(anotherBid); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.delete(aBid); session.delete(anotherBid); session.saveOrUpdate(anItem); tx.commit(); session.close(); Automatic deletion of orphans saves you two lines of code two lines of code that are inconvenient. Without orphan deletion, you d have to remember all the Bid
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply