Domain Model:
Relational Data Model
The n-n association must be similar to the 1-n ing. The key sub-element must be added to the set element, and the foreign key of the CATEGORIES_ITEMS table referenced in the CATEGORIES table must be CATEGORIY_ID. different from 1-n Association ing, when a n-n association is established, the elements in the Set use counter-to-minus. the class attribute of the sort-to-Sort sub-element specifies that the Item object is stored in the items set, and the column attribute specifies that the foreign key of the ITEMS table in the CATEGORIES_ITEMS table is ITEM_ID.
Example: Item. java
package com.atguigu.hibernate.n2n;import java.util.HashSet;import java.util.Set;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;}}
Category. java
package com.atguigu.hibernate.n2n;import java.util.HashSet;import java.util.Set;public class Category {private Integer id;private String name;private Set
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
getItems() {return items;}public void setItems(Set
items) {this.items = items;}}
Item. hbm. xml
Category. hbm. xml