hibernate双向多对多关联

一.

注意:1.在持久化类里面写set的时候一定要初始化,否则会出现初始化问题

    2.在类关联文件中要设置

hibernate双向多对多关联_第1张图片

二.代码

1.AItem类

package cn.edu.sdut.hibernate.nton;

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

public class AItem {

	private int aId;
	private String aName;
	
	private Set bSet = new HashSet<>();//注意为set初始化,否则会出现空指针异常

	public int getaId() {
		return aId;
	}

	public void setaId(int aId) {
		this.aId = aId;
	}

	public String getaName() {
		return aName;
	}

	public void setaName(String aName) {
		this.aName = aName;
	}

	public Set getbSet() {
		return bSet;
	}

	public void setbSet(Set bSet) {
		this.bSet = bSet;
	}


	
}
2.BItem类

package cn.edu.sdut.hibernate.nton;

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


public class BItem {

	private int bId;
	private String bName;
	private Set aSet = new HashSet<>();//注意为set初始化,否则会出现空指针异常
	
	public Set getaSet() {
		return aSet;
	}
	public void setaSet(Set aSet) {
		this.aSet = aSet;
	}
	public int getbId() {
		return bId;
	}
	public void setbId(int bId) {
		this.bId = bId;
	}
	public String getbName() {
		return bName;
	}
	public void setbName(String bName) {
		this.bName = bName;
	}
	
	
}

3.AItem.hbm.xml





    
        
            
            
        
        
            
        
        
        
        
            
            	
                
            
            
            
        
    


4.BItem.hbm.xml





    
        
            
            
        
        
            
        
        
         
            
            	
                
            
            
            
        
         
    

5.hibernate.cfg.xml




    
    	
    	
    	root
    	csc
    	com.mysql.jdbc.Driver
    	jdbc:mysql:///hibernate5
    
    	
    	
    	org.hibernate.dialect.MySQLInnoDBDialect
    	
    	
    	true
    	
    	
    	true
    	
    	
    	update
    	
    	
    	true
    	
    	
    	2
    	
    	
    	100
    	20
    	5
    	2000
    	2000
    	10
    	
    	
    	100
    	
    	30
    	
    	
    	
    	
    	
    	
    	
    	
    


6.TestNtoN类

package cn.edu.sdut.hibernate.nton;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestNtoN {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	
	@Test
	public void testSave(){
		AItem aItem = new AItem();
		aItem.setaName("AA1");
		AItem aItem2 = new AItem();
		aItem2.setaName("AA2");
		BItem bItem = new BItem();
		bItem.setbName("BB1");
		BItem bItem2 = new BItem();
		bItem2.setbName("BB2");
		
		aItem.getbSet().add(bItem);
		aItem.getbSet().add(bItem2);
		aItem2.getbSet().add(bItem);
		aItem2.getbSet().add(bItem2);
		
		bItem.getaSet().add(aItem);
		bItem.getaSet().add(aItem2);
		bItem2.getaSet().add(aItem);
		bItem2.getaSet().add(aItem2);
		
		session.save(aItem);
		session.save(aItem2);
		session.save(bItem);
		session.save(bItem2);
	}
	
	@Test
	public void testGet(){
		AItem aItem = (AItem) session.get(AItem.class, 1);
		System.out.println(aItem.getbSet());
		BItem bItem = (BItem) session.get(BItem.class, 1);
		System.out.println(bItem.getaSet().getClass().getName());
	}
}




你可能感兴趣的:(Hibernate)