Home > Java > javaTutorial > Detailed explanation of multi-table mapping relationship configuration in hibernate

Detailed explanation of multi-table mapping relationship configuration in hibernate

怪我咯
Release: 2017-06-25 10:00:53
Original
1400 people have browsed it

1. One-to-many one-to-many relationship mapping configuration (configured in one entity mapping file)

<br>
Copy after login
    <!-- 
            cascade属性:级联操作属性
                save-update: 级联保存,保存客户时,级联保存客户关联的联系人
                delete:级联删除,删除客户时,级联删除客户关联的联系人
                all:级联保存+级联删除         --> <!-- 
            inverse属性:设置是否不维护关联关系
                true:不维护关联
                false(默认值):维护关联         --><!-- 一对多 --><set name="linkMen" inverse="true" ><!-- 外键列名 --><key column="lkm_cust_id" ></key><!-- 该集合是一对多关系表达,关联的对象时linkman --><one-to-many class="LinkMan" /></set>
Copy after login

One pair Many|many-to-one relationships, place SQL statements redundantly. Generally, the party that chooses one gives up maintenance, and the inverse attribute is set to true.

2.many-to-one many-to-one relationship mapping configuration (in many Configured in the entity mapping file of one party)

            <!-- 
            cascade属性:级联操作属性
                save-update: 级联保存,保存客户时,级联保存客户关联的联系人
                delete:级联删除,删除客户时,级联删除客户关联的联系人
                all:级联保存+级联删除         --> <!-- 
             没有inverse属性:
                 外键列所在实体,无法放弃维护关联关系.          --><!-- 多对一 --><many-to-one name="customer"    
         column="lkm_cust_id" 
         class="Customer" ></many-to-one>
Copy after login

3.many-to-many many-to-many relationship mapping configuration

                <!-- 多对多关系配置 
                table:中间表表名--> <!-- 
            inverse属性:设置是否不维护关联关系
                true:不维护关联
                false(默认值):维护关联         --> <!-- 
            cascade属性:级联操作属性
                save-update: 级联保存,保存客户时,级联保存客户关联的联系人
                delete:级联删除,删除客户时,级联删除客户关联的联系人
                all: 级联保存+级联删除         --><set name="roles" table="sys_user_role"   ><!-- 别人引用"我"的外键列名 --><key column="user_id" ></key><!-- 表达集合是多对多关系
                class属性:表达我与谁是多对多
                column属性:表达另外一个外键列名             --><many-to-many class="Role" column="role_id" ></many-to-many></set>
Copy after login

many-to-many In the relationship, select one party to initiate maintenance of the relationship, place duplicate data entry in the intermediate table, and decide based on business logic. For example, if the product and order have a many-to-many relationship, the order maintenance product will give up maintenance

The above is the detailed content of Detailed explanation of multi-table mapping relationship configuration in hibernate. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template