CHAPTER 12 Modifying objects efficiently Creating a new (Most popular web site)
CHAPTER 12 Modifying objects efficiently Creating a new category in a detached fashion Let s do the same thing again, but this time create the link between Computer and Laptops outside of the persistence context scope: Category computer = (Category) session.get() // Loaded in previous Session Category laptops = new Category(”Laptops”); computer.getChildCategories().add(laptops); laptops.setParentCategory(computer); You now have the detached fully initialized (no proxy) computer object, loaded in a previous Session, associated with the new transient laptops object (and vice versa). You make this change to the objects persistent by saving the new object in a second Hibernate Session, a new persistence context: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // Persist one new category and the link to its parent category session.save(laptops); tx.commit(); session.close(); Hibernate inspects the database identifier property of the laptops.parentCategory object and correctly creates the reference to the Computer category in the database. Hibernate inserts the identifier value of the parent into the foreign key field of the new Laptops row in CATEGORY. You can t obtain a detached proxy for computer in this example, because computer.getChildCategories() would trigger initialization of the proxy and you d see a LazyInitializationException: The Session is already closed. You can t walk the object graph across uninitialized boundaries in detached state. Because you have cascade=”none” defined for the parentCategory association, Hibernate ignores changes to any of the other categories in the hierarchy (Computer, Electronics)! It doesn t cascade the call to save() to entities referred by this association. If you enabled cascade=”save-update” on the
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.