Hibernate unidirectional n-n

Source: Internet
Author: User

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
 
     
              
               
                
            
               
               
            
                   
           
               
                    
                 
                
                
            
               
  
 


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.