Proficient in hibernate--mapping one-to-many bidirectional self-correlation relationships

Source: Internet
Author: User

First we look at:

Each category represents a Category object, each category can be associated with a parent list, and can also be associated with a subcategory, in order to express this one-to-many bidirectional self-correlation, you can define two attributes in the category class:
Parentcategory: Referencing parent category objects
ChildCategories: Referencing a group of sub-category objects

Here's a look at the source code for category:

publicclass Category implements Serializable{    private Lang id;    private String name;    private Category parentCategory;    private Set childCategories;    ....// get,set方法略,手写的}

The mapping file for category classes is as follows:

        <set name="childCategories" cascade="save-update" inverse="true">            <key column="category_id" />            <one-toclass="com.fyw.Category" />        </set>

Let's analyze the steps to perform cascading operations:
(1) Savecategorywidthcascade (): When the cascade of the Set property is Save-update:

tx = Session.begintransaction (); Category foodcategory =NewCategory ("Food",NULL,NewHashSet ()); Category fruitcategory =NewCategory ("Fruit",NULL,NewHashSet ()); Category vegetablecategory =NewCategory ("Vegetable",NULL,NewHashSet ()); Category applecategory =NewCategory ("Apple",NULL,NewHashSet ()); Category orangecategory =NewCategory ("Orange",NULL,NewHashSet ()); Category tomatocategory =NewCategory ("Tomato",NULL,NewHashSet ());//Establish the relationship between food and fruitFoodcategory.getchildcategories (). Add (fruitcategory); fruitcategory.setparentcategory (foodCategory);//Establish the relationship between food and vegetablesFoodcategory.getchildcategories (). Add (vegetablecategory); vegetablecategory.setparentcategory (foodCategory);//Establish a relationship between fruit and applesFruitcategory.getchildcategories (). Add (applecategory); applecategory.setparentcategory (fruitCategory);//Establish a relationship between fruit and orangesFruitcategory.getchildcategories (). Add (orangecategory); orangecategory.setparentcategory (fruitCategory);//Establish a relationship between tomatoes and fruitsFruitcategory.getchildcategories (). Add (tomatocategory); tomatocategory.setparentcategory (fruitCategory); Session.save (foodcategory); Tx.commit ();

When set CASCADE is Save-update, Hibernate automatically persists other category objects that are associated when the category object is persisted.
(2) Modifycategoryassociation (): This method demonstrates how to modify the relationship between associated objects. The code is as follows:

tx = Session. BeginTransaction();Category tomatocategory = Findcategorybyname (Session,"Tomato");Category fruitcategory = Findcategorybyname (Session,"Fruit");Category vegetablecategory = Findcategorybyname (Session,"Vegetable");Establish the relationship between tomatoes and vegetables vegetablecategory. Getchildcategories(). Add(tomatocategory);Tomatocategory. SetParentCategory(vegetablecategory);Remove the relationship between tomatoes and fruit fruitcategory. Getchildcategories(). Remove(tomatocategory);Tx. Commit();

First, the Tomatocategory,fruitcategory and Vegetablecatgory objects are loaded by the Findcategorybyname method and returned to match the requirements:

publicfindCategoryByName(Session session,String name)throws Exception{    List results = session.find("from Category as c where c.name =‘"+name+"‘");    return (Category)results.iterator().next();}

The Findcategorybyname method and the Modifycategoryassociation method share a session instance, and the category returned by Findcategorybyname is still persisted.
The Modifycategoryassociation method updates the association between these three objects, and hibernate updates the database automatically as their state changes when hibernation cleans up objects that are persisted in the cache.
Although the three objects in the cache are Tomcatocategory,fruitcategory,vegetablecategory, the set's Inverse property is set to True,hibernate to execute only one SQL statement.

Proficient in hibernate--mapping one-to-many bidirectional self-correlation relationships

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.