Hibernate's configuration file works and two designs: one-to-many, many-to-many

Source: Internet
Author: User
"One-to-many" is the most common mapping relationship, simply like the relationship between consumers and orders. One-to-many: from the consumer corner of the degree of a consumer can have multiple orders, that is, a pair of more. Many-to-one: from the point of order, multiple orders can correspond to a consumer, that is, many to one. Personal simple little summary.

One-to-many

Entity class

A: Private set<linkman> Linkmans; Express one-to-many relationship: private customer customer; Express a many-to-one relationship

Configuration file

One:

    <!--collection, one-to-many relationships, configured in configuration Files-    <!--         Name property:   Collection property name        Column property: Foreign key Column Name        class Property:  Objects associated with me full class name     -<!--          Cascade operation: Cascade property             save-update: Cascade Save update             Delete:      cascade Delete all             :         save-update+delete         Cascade Operations: simplifies operations. The goal is to reduce the two lines of code.      -<!--inverse properties: Configures whether relationships are maintained.                  True:          customer does not maintain relationship                 false (default): Customer maintains relationship          Inverse property: Performance optimization. Improve the performance of relationship maintenance.          Principle: No matter how to give up, there must always be a party to maintain the relationship.          In a one-to-many relationship: a party gives up. Only one side can give up. Many of the parties cannot give up.      --    <set name= "Linkmens" inverse= "true" cascade= "delete" >        <key column= "lkm_cust_id" ></ key>        <one-to-many class= "Linkman"/>    </set>

Many:

    <!--Many-to-one    <!--         Name property:   Reference Property name        Column property: Foreign key Column Name        Class Property:  object associated with me full class name--     <!--          CASCADE operations: Cascade Property             save-update: Cascade Save update             Delete:      cascade Delete all             :         save-update+ Delete         cascade operation: simplifies the operation. The goal is to have less than two lines of code.      -<!--Many parties: You cannot abandon the maintenance relationship. The Foreign key field is on more than one side.  --    <many-to-one name= "Customer" column= "lkm_cust_id" class= "Customer" ></many-to-one>

Many-to-many

Entity class

MORE: Private set<user> users; Express many-to-many relationships: private set<role> roles; Express many-to-many relationships

Configuration file
Many:

    <!--Many-to-many relationships-    <!--         Name: Collection Property name        table: Configuring the Intermediate Table name key column         : Foreign key, the name        of the foreign key that others refer to "I" Many-to-many class         :   which Class I am with is a many-to-many relationship         column:  foreign key. I'm referencing someone else's foreign key column name-    <!--using the inverse property        true: Discard maintain foreign key relationship        false (default): Maintain relationship            Conclusion: In the future, if you encounter many-to-many relationships in development. Be sure to choose one side to abandon the maintenance relationship.         Generally who will give up to see the direction of business. For example, when entering an employee, you need to assign a role to the employee.         Then the business direction is the employee maintenance role. Roles do not require maintenance and employee relationships. Role Waiver Maintenance-            <set name= "users" table= "Sys_user_role" inverse= "true" >        <key Column= "role_id" ></key>        <many-to-many class= "User" column= "user_id" ></many-to-many>    </set>

Many:

 <!--many-to-many relationships-<!--Name: Collection property Name table: Configuring intermediate table names     Key column: Foreign Key, others refer to "I" foreign key columns name Many-to-many class: Which Class I and which is a many-to-many relationship column: foreign key. I quote someone else's foreign key column name -<!--Cascade Cascade operation: save-update: Cascade Save Update Delete: Cascade Delete al L: Cascade Save update + CASCADE Delete Conclusion: Cascade simplifies code writing. This property makes it irrelevant.              It is recommended to use only save-update.        If using the delete operation is too dangerous. Especially in many-to-many. Not recommended.--<set name= "Roles" table= "Sys_user_role" cascade= "Save-update" >     <key column= "user_id" ></key> <many-to-many class= "Role" column= "role_id" ></many-to-many> </set> 
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.