Hibernate bidirectional n-n

Source: Internet
Author: User

Domain Model:

Relational Data Model

Two-way n-n association requires both ends to use the set property two-way n-n association must use the connection table set property should add key sub-elements to map foreign key columns, in the set element, you should also add the correlated-to-correlated sub-element to associate the object class. On both sides of the bidirectional n-n association, you must specify the name of the table connecting to the table and the name of the foreign key column. the values of the table element in the set element must be specified and must be the same. The column attribute must be specified for the key and pair-to-pair sub-elements of the set element, key and keep-to-join specify the foreign key column names of the persistence class and associated class in the connection table respectively. Therefore, the column attributes of the keys and keep-to-join classes on both sides are the same. That is to say, the cloumn value of the key of the set element on one side is a, and the column of the keys-to-minus is B; then the column value of the key of the set element on the other side is B, the column value of replicate-to-sequence is. for bidirectional n-n associations, the inverse at one end must be set to true. Otherwise, maintaining associations at both ends may cause primary key conflicts.



Code details: 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. java
package com.atguigu.hibernate.n2n;import java.util.HashSet;import java.util.Set;public class Item {private Integer id;private String name;private Set
 
   categories = 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
  
    getCategories() {return categories;}public void setCategories(Set
   
     categories) {this.categories = categories;}}
   
  
 

Category. hbm. xml
 
     
              
               
                
            
               
               
            
                   
           
               
                    
                 
                
                
            
               
  
 

Item. hbm. xml
 
     
                  
               
                
            
                   
               
            
                   
           
            
            
               
  
 


Package com. atguigu. hibernate. n2n; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. SQL. blob; import java. SQL. connection; import java. SQL. SQLException; import java. util. date; import java. util. set; import org. hibernate. hibernate; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. jdbc. work; import org. hibernate. service. serviceRegistry; import org. hibernate. service. serviceRegistryBuilder; import org. junit. after; import org. junit. before; import org. junit. test; public class HibernateTest {private SessionFactory sessionFactory; private Session session; private Transaction transaction; @ Beforepublic void init () {Configuration configuration Configuration = new Configuration (). configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (configuration. getProperties ()). buildServiceRegistry (); sessionFactory = configuration. buildSessionFactory (serviceRegistry); session = sessionFactory. openSession (); transaction = session. beginTransaction () ;}@ Afterpublic void destroy () {transaction. commit (); session. close (); sessionFactory. close () ;}@ Testpublic void testGet () {Category category = (Category) session. get (Category. class, 1); System. out. println (category. getName (); // you need to connect to the intermediate table Set
 
  
Items = category. getItems (); System. out. println (items. size () ;}@ Testpublic void testSave () {Category category1 = new Category (); category1.setName ("C-AA"); Category category2 = new Category (); category2.setName ("C-BB"); Item item1 = new Item (); item1.setName ("I-AA"); Item item2 = new Item (); item2.setName ("I-BB "); // set the association relationship category1.getItems (). add (item1); category1.getItems (). add (item2); category2.getItems (). add (item1); category2.getItems (). add (item2); item1.getCategories (). add (category1); item1.getCategories (). add (category2); item2.getCategories (). add (category1); item2.getCategories (). add (category2); // execute the save operation session. save (category1); session. save (category2); session. save (item1); session. save (item2 );}}
 


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.