Transitive persistence Saving several new instances with transitive (Cheapest web hosting)
Transitive persistence Saving several new instances with transitive persistence Why do we have cascading operations? You could save the laptop object, as shown in the previous example, without using any cascade mapping. Well, consider the following case: Category computer = … // Loaded in a previous Session Category laptops = new Category(”Laptops”); Category laptopUltraPortable = new Category(”Ultra-Portable”); Category laptopTabletPCs = new Category(”Tablet PCs”); laptops.addChildCategory(laptopUltraPortable); laptops.addChildCategory(laptopTabletPCs); computer.addChildCategory(laptops); (Notice that the convenience method addChildCategory() sets both ends of the association link in one call, as described earlier in the book.) It would be undesirable to have to save each of the three new categories individually in a new Session. Fortunately, because you mapped the childCategories association (the collection) with cascade=”save-update”, you don t need to. The same code shown earlier, which saved the single Laptops category, will save all three new categories in a new Session: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // Persist all three new Category instances session.save(laptops); tx.commit(); session.close(); You re probably wondering why the cascade style is called cascade=”save-update” rather then merely cascade=”save”. Having just made all three categories persistent previously, suppose you make the following changes to the category hierarchy in a subsequent event, outside of a Session (you re working on detached objects again): laptops.setName(”Laptop Computers”); // Modify laptopUltraPortable.setName(”Ultra-Portable Notebooks”); // Modify laptopTabletPCs.setName(”Tablet Computers”); // Modify Category laptopBags = new Category(”Laptop Bags”); laptops.addChildCategory(laptopBags); // Add You add a new category (laptopBags) as a child of the Laptops category, and modify all three existing categories. The following code propagates all these changes to the database:
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.