Transitive persistence . Both refer to the same foreign key column PARENT_CATEGORY_ID. All columns are in the same table, CATEGORY. Creating a new category Suppose you create a new Category, as a child category of Computer; see figure 12.3. You have several ways to create this new Laptops object and save it in the database. You can go back to the database and retrieve the Computer category to which the new Laptops category will belong, add the new category, and commit the transaction: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Category computer = (Category) session.load(Category.class, computerId); Category laptops = new Category(”Laptops”); computer.getChildCategories().add(laptops); laptops.setParentCategory(computer); tx.commit(); session.close(); The computer instance is persistent (note how you use load() to work with a proxy and avoid the database hit), and the childCategories association has cascade- save enabled. Hence, this code results in the new laptops category becoming persistent when tx.commit() is called, as Hibernate cascades the persistent state to the childCategories collection elements of computer. Hibernate examines the state of the objects and their relationships when the persistence context is flushed and queues an INSERT statement. Figure 12.3 Adding a new Category to the object graph
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.
This entry was posted
on Tuesday, December 11th, 2007 at 4:01 pm and is filed under j2ee.
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.