Proficient in Hibernate-cascade objects

Source: Internet
Author: User

Proficient in Hibernate-cascade objects

When we load a class, the session will load all objects directly or indirectly associated with the class.
In the object relationship ing file, elements used to map the associations between persistence classes, such as, and elements, all have a cascade attribute, which specifies how to manipulate the currently associated objects, its optional attributes are as follows:
None: when an object is saved, updated, or deleted, other associated objects are ignored. This is the default attribute of cascade.
Save-update: when the current object is saved or updated through the save, update, and saveOrUpdate methods of the session, cascade all associated temporary objects to be saved, and cascade updates of all associated free objects
Delete: when the current object is deleted using the delete method of the session, all associated objects are deleted cascade.
All: Except save-update and delete actions, evict or lock operations on the current object will also be performed on the associated Persistent Object.
Delete-orphan: delete all objects that are not associated with the current object.
All-delete-orphan: contains all and delete-orphan actions
Take the following relationship as an example:

1. Cascade temporary objects for saving
SaveFoodCategZ success? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnkoKbe9t6jPyLS0vajI/Workshop/OhozwvcD4NCjxwcmUgY2xhc3M9" brush: java; ">Public void saveFoodCategory () throws Exception {Category foodCategory = new Category (food, null, new Hashset (); Category fruitCategory = new Category (fruit, null, new Hashset ()); category appleCategory = new Category (apple, null, new Hashset (); // establishes the relationship between the food Category and the fruit Category foodCategory. addChildCategory (fruitCategory); // establishes the relationship between the fruit category and the Apple category. addChildCategory (appleCategory); saveOrUpdate (foodCategory );}

The saveOrUpdate method calls the saveOrUpdate method of the session to save the foodCategory object. Perform the following steps:
1. Because foodCatogory is a temporary object, session calls the save method to save the foodCategory object.
2. The session uses the foodCategory. getChildCategories method to navigate to the fruitCategory temporary object and call the save method to save the fruitCategroy temporary object.
3. The session uses the fruitCategory. getChildCategories method to navigate to the appleCategory object and call the save method to save the temporary appleCategory object.
The Session executes three insert statements when clearing the cache.

insert into categories (name,category_id,id) values(food,null,1);insert into categories (name,category_id,id) values(fruit,1,2);insert into categories (name,category_id,id) values(food,2,3);

2. Update persistence objects
The saveOrangeCategory () method is used to persist an orangeCategory object. It first calls the findCategoryByName (session, "fruit") method. Because the query method shares a session with the saveOrangeCategory () method, the returned fruitCategory object is in the persistent state. Next, create an orangeCategory temporary object to establish the association between fruitCategory and orangeCategory.
3. Persistence of temporary objects
The saveVegetableCategory () method is used to persist a temporary vegetableCategory object.
The saveVegetableCategory () method first calls the findCategoryByName ("food") method. This method does not share a session with the saveVegetableCategory () method, so the returned foodCategory object is in the Free State. Next, create a temporary vegetableCategory object to establish the association between foodCategory and vegetableCategory. Finally, saveOrUpdate () is called to save the vegetableCategory object:

Public void saveVegetableCategory () throws Exception {// The returned foodCategory is the free object Category foodCategory = findCategoryByName (food); Category vegetableCategory = new Category (vegetable, null, new HashSet ()); // The free object of foodCategory is associated with the temporary object of vegetableCategory. addChildCategory (vegetableCategory); saveOrUpdate (vegetableCategory );}

The saveOrUpdate method calls the saveOrUpdate of the Session To save the vegetableCategory object. The saveOrUpdate of the Session performs the following steps:
1. Because vegetableCategory is a temporary object, the Session calls the save method to persist the vegetableCategory object.
2. The Session uses the vegetableCategory. getParentCategory () method to navigate to the foodCategory object. Because the foodCategory object is a free object, the update method is called to update the foodCategory object.
3. The Session uses the foodCategory. getChildCategories method to navigate to the fruitCategory object. Because the fruitCategory object is a free object, the update method is called to update the fruitCategory object.
4. Update the free object
The updateVegetableCategory method is used to update the name attribute of the vegetableCategory object and create a tomatoCategory object, which is associated with the vegetableCategory object.
The updateVegetableCategory method first calls the findCategoryByName ("vegetable") method. The query method does not share a Session with the updateVegetableCategory method, so the returned vegetableCategory object is in the Free State. Next, modify the name attribute of the vegetableCategory object, create a temporary tomatoCategory object, establish the association between vegetableCategory and tomatoCategory, and call the saveOrUpdate method to mail the vegetableCategory object.

Public void updateVegetableCategory () throws Exception {// The returned vegetableCategory is the free object Category vegetableCategory = findCategoryByName (vegetable); vegetableCategory. setName (green vegetable); Category tomatoCategory = new Category (tomato, null, new HashSet (); // correlation between the vegetableCategory free object and the tomato temporary object vegetableCategory. addChildCategory (tomatoCategory); saveOrUpdate (vegetableCategory );}

5. Traverse object Graphs
The navigateCategories method first calls the findCategoryByName ("fruit") method. Because the query method does not share a Session with navegateCategories, the returned fruitCategory object is in the Free State. Next, call navigateCategories (fruitCategory, categories) to obtain all associated Category free objects.

Public void navigateCategores () throws Exception {Category fruitCategory = findCategoryByName (fruit); HashSet categories = new HashSet (); navigateCategories (fruitCategory, categories ); // print all Category objects in the categories set for (Iterator it = categories. iterator (); it. hasNext ();) {System. out. println (Category) it. next ()). getName ());}}

The navigateCategories method uses recursive algorithms to traverse all parent class and Category objects associated with fruitCategory and store them in a Set:

Private void navigateCategories (Category category, Set categories) {if (categories. contains (category) | category = null) return; categories. add (category); // recursively traverse the parent class Category navigateCategories (category. getParentCategory (), categories); Set childCategories = category. getChildCategores (); if (childCategories = null) return; // recursively traverse all subclass Category for (Iterator it = childCategores. interator (); it. hasNext ();) {navigateCategories (Category) it. next (), categories );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.