Proficient in hibernate---controlled objects

Source: Internet
Author: User
Tags set set

When we load a class, the session loads all objects that are directly or indirectly associated with that class.
In an Object relational mapping file, elements, such as, and elements, that are used to map associations between persisted classes, have an Cascade property that specifies how to manipulate the object that is currently associated with it, with the following optional properties:
None: Ignores other associated objects when you save, update, or delete an object, which is the Cascade default property
Save-update: When the current object is saved or updated through the Save, update, and Saveorupdate methods of the session, Cascade saves all the associated new temporary objects, and cascade updates all associated free objects
Delete: Cascade deletes all associated objects when the current object is deleted through the Delete method of the session
All: A evict or lock operation is also performed on the associated persisted object when the evict or lock operation is performed on the current object, including save-update and delete behavior
Delete-orphan: Delete all objects that disassociate from the current object
All-delete-orphan: Contains all and Delete-orphan behavior
Let's take the following class of relationships as an example:

1. Cascade Save Temporary objects
The Savefoodcategory () method first creates three category temporary objects, establishes their association relationships, and then calls the Saveorupdate method to save the Foodcategory object.

 Public void savefoodcategory()throwsexception{Category foodcategory =NewCategory ("Food",NULL,NewHashset ()); Category fruitcategory =NewCategory ("Fruit",NULL,NewHashset ()); Category applecategory =NewCategory ("Apple",NULL,NewHashset ());//Establish the relationship between food category and fruit categoryFoodcategory.addchildcategory (fruitcategory);//Establish the relationship between fruit category and Apple categoryFruitcategory.addchildcategory (applecategory); Saveorupdate (foodcategory);}

The Saveorupdate method invokes the Saveorupdate method of the session to save the Foodcategory object. The following steps are performed:
1. Because Foodcatogory is a temporary object, the session calls the Save method to save the Foodcategory object
2.session navigates to the Fruitcategory temporary object through the Foodcategory.getchildcategories method, calling the Save method to save the Fruitcategroy temporary object
3.session navigates to the Applecategory Pro object through the Fruitcategory.getchildcategories method, calling the Save method to save the Applecategory temporary object
Session executes three INSERT statements while cleaning up 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 );   

The

2, update persisted object
Saveorangecategory () method is used to persist a Orangecategory object, and he will first invoke the Findcategorybyname (session, "fruit") method, Because the query method is common to a session with the Saveorangecategory () method, the Fruitcategory object that he returns is in a persisted state. Next, create a orangecategory temporary object that establishes the association between Fruitcategory and Orangecategory.
3, persistent temporary object
Savevegetablecategory () method to persist a vegetablecategory temporary object
Savevegetablecategory () Method first calls the Findcategorybyname ("food") method, which does not share a session with the Savevegetablecategory () method, so the returned Foodcategory object is in a free state. Next, create a vegetablecategory temporary object that establishes the association between Foodcategory and Vegetablecategory. Finally, call Saveorupdate () to save the Vegetablecategory object:

public   void  savevegetablecategory  () throws  exception{//returned Foodcategory is a free object  Category foodcategory = Findcategorybyname (
    
      "food" ); Category vegetablecategory = 
     new  category (
      "vegetable"    , 
     null , 
     new  HashSet ()); 
     //foodcategory free object associated with vegetablecategory temporary object  foodcategory.addchildcategory (    Vegetablecategory); Saveorupdate (vegetablecategory);} 
     

The

Saveorupdate method calls the saveorupdate of the session to save the Vegetablecategory object, and 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.Session navigates to the Foodcategory object through the Vegetablecategory.getparentcategory () method, because the Foodcategory object is a free object, Therefore, call the Update method to update the Foodcategory object.
3. The session navigates to the Fruitcategory object through the Foodcategory.getchildcategories method, because the Fruitcategory object is a free object, so calling the Update method updates the Fruitcategory object
4, Update free objects
The Updatevegetablecategory method is used to update the Name property of the Vegetablecategory object, and to create a Tomatocategory object, He associates the
Updatevegetablecategory method with the Vegetablecategory object by calling the Findcategorybyname ("vegetable") method first. The query method does not have a common session with the Updatevegetablecategory method, so the returned Vegetablecategory object is in a free state. Next, modify the Name property of the Vegetablecategory object, and then create a tomatocategory temporary object that establishes an association between Vegetablecategory and Tomatocategory. Last Call Saveorupdate method Vegetablecategory object

public   void  updatevegetablecategory  () throws  exception{//returned Vegetablecategory is a free object  Category vegetablecategory = Findcategoryb    Yname ( "vegetable" );    Vegetablecategory.setname ( "green vegetable" ); Category tomatocategory = new  category ( "tomato"     , null , new  HashSet ()); //vegetablecategory the free object associated with the tomato temporary object  vegetablecategory.addchildcategory (    Tomatocategory); Saveorupdate (vegetablecategory);}  

5. Traverse the object graph
The Navigatecategories method calls the Findcategorybyname ("fruit") method first, because the query method does not have a session with the navegatecategories. So the Fruitcategory object He returned is in a free state. Next Call Navigatecategories (fruitcategory,categories) to get all the associated category free objects

   publicvoidnavigateCategores() throws Exception{    Category fruitCategory = findCategoryByName("fruit");    new HashSet();    navigateCategories(fruitCategory,categories);    //打印categories集合中所有的Category对象    for(Iterator it = categories.iterator();it.hasNext();){        System.out.println(((Category)it.next()).getName());    }}

The

Navigatecategories method uses a recursive algorithm to traverse all parent categories and categories that are associated with fruitcategory and to store them in a set set:

privatevoidnavigateCategories(Category category,Set categories){    ifnullreturn;    categories.add(category);    //递归遍历父类Category    navigateCategories(category.getParentCategory(),categories);    Set childCategories = category.getChildCategores();    ifnullreturn;    //递归遍历所有子类Category    for(Iterator it = childCategores.interator();it.hasNext();){        navigateCategories((Category)it.next(),categories);    }}

Proficient in hibernate---controlled objects

Related Article

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.