7.6 hibernate mapping: one way many to many Association

hibernate mapping - one way many to many Association

  • n-n association must use join table
  • Similar to the 1-n mapping, the set element must be set to add a key sub element, and the foreign key referring to the CATEGORIES table in the CATEGORIES & ITMS table must be the CATEGORIES & ID. different from the 1-n association mapping, when the n-n association is established, the elements in the set use many to many. The class attribute of many to many sub element specifies that the Item object is stored in the items collection, and the column attribute specifies that the foreign key in the CATEGORIES table refers to the Item table ID in the items table

1, Code example:

Category.class

package com.zit.hibernate.n2n;

import java.util.HashSet;
import java.util.Set;

public class Category {
    
    private Integer id;
    private String name;
    
    private Set<Item> items = new HashSet<>();
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set<Item> getItems() {
        return items;
    }
    public void setItems(Set<Item> items) {
        this.items = items;
    }
    
}
Item.class

package com.zit.hibernate.n2n;

public class Item {
    
    private Integer id;
    private String name;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

(unidirectional n-n)

Mapping files

Item.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-25 13:55:35 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.zit.hibernate.n2n">
    <class name="Item" table="ITEMS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
    </class>
</hibernate-mapping>
Category.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-25 13:55:35 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.zit.hibernate.n2n">
    <class name="Category" table="CATEGORIES">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <set name="items" table="CATEGORIES_ITEMS">
            <key>
                <column name="C_ID" />
            </key>
            <many-to-many class="Item" column="I_ID"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
In Category.hbm.xml,

 <set name="items" table="CATEGORIES_ITEMS">
    <key>
       <column name="C_ID" />
    </key>
    <many-to-many class="Item" column="I_ID"></many-to-many>
 </set>

The table here specifies the name of the intermediate table, the key sub element, and the foreign key of the intermediate table referring to the CATEGORIES table is C ﹣ ID

When using many to many, the class attribute specifies that the Item object is stored in the items collection, and the column specifies the foreign key in the intermediate table that references the items table

It's called "I" ID.

(if the intermediate table does not specify a primary key, then the two properties are combined as the primary key.)

2, Note for use:

1. When saving records, since there is no direct connection between the two tables, there is no direct impact on which data is save d successively. But when saving records, you need to operate the intermediate table at the same time, so there will be more insert statements.

2. When querying, there is lazy loading. When querying the size of set < item >, the intermediate table and ITEMS table will be connected internally.


Tags: Hibernate Java xml Attribute

Posted on Sun, 03 May 2020 14:04:04 -0700 by Waldir