hibernate中多对多映射配置详细解析

1.hibernate.cfg.xml文件


 

    
	
		
		com.mysql.jdbc.Driver
		jdbc:mysql:///hib-demo
		root
		123456
		
		org.hibernate.dialect.MySQL5Dialect
		
		
		   
		true
		   
		
		update
		
	

2.Developer

package cn.itcast.c_many2many;
 
import java.util.HashSet;
import java.util.Set;
 
//开发人员信息
public class Developer {
	private Integer d_id;//开发人员编号
	private String d_name;//开发人员姓名
	//开发人员参与的多个项目
	private Set projects=new HashSet();
	public Integer getD_id() {
		return d_id;
	}
	public void setD_id(Integer d_id) {
		this.d_id = d_id;
	}
	public String getD_name() {
		return d_name;
	}
	public void setD_name(String d_name) {
		this.d_name = d_name;
	}
	public Set getProjects() {
		return projects;
	}
	public void setProjects(Set projects) {
		this.projects = projects;
	}
	
	
 
}

3.Project

package cn.itcast.c_many2many;
 
import java.util.HashSet;
import java.util.Set;
 
public class Project {
	private Integer prj_id;//项目编号
	private String prj_name;//项目名称
	//项目下的多个员工
	private Set developers=new HashSet();
	public Integer getPrj_id() {
		return prj_id;
	}
	public void setPrj_id(Integer prj_id) {
		this.prj_id = prj_id;
	}
	public String getPrj_name() {
		return prj_name;
	}
	public void setPrj_name(String prj_name) {
		this.prj_name = prj_name;
	}
	public Set getDevelopers() {
		return developers;
	}
	public void setDevelopers(Set developers) {
		this.developers = developers;
	}
	
	
	
 
}

4.Developer.hbm.xml



 




	
	    
	       
	    
	    
	   
	    
	       
	       
	    
	    
	     
	
	
 

5.Project.hbm.xml



 




	
	    
	       
	    
	    
	    
	     
	    
	        
	        
	    
	    
	    
	     
	
	
 

6.junit测试

package cn.itcast.c_many2many;
 
 
 
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
 
public class App_save {
	private static SessionFactory sf;
	static{
		sf=new Configuration()
		.configure()
		.addClass(Project.class)
		.addClass(Developer.class)  //测试的时候使用
		.buildSessionFactory();
	}
	
	//多对多数据的保存,只能通过一方维护另一方,不能重复维护!
	@Test
	public void Save() {
		Session session=sf.openSession();
		session.beginTransaction();
	    //创建项目对象
		Project prj_ds=new Project();
		prj_ds.setPrj_name("电商系统");
		Project prj_oa=new Project();
		prj_oa.setPrj_name("OA系统");
		//创建员工对象
		Developer dev_cj=new Developer();
		dev_cj.setD_name("刘德华");
		Developer dev_wc=new Developer();
		dev_wc.setD_name("高园园");
		Developer dev_lz=new Developer();
		dev_lz.setD_name("王明");
		//关系
		prj_ds.getDevelopers().add(dev_cj);
		prj_ds.getDevelopers().add(dev_wc);//电商系统 :刘德华,高园园
		prj_oa.getDevelopers().add(dev_cj);
		prj_oa.getDevelopers().add(dev_lz);//OA系统:刘德华,王明
		//保存
		session.save(dev_cj);
		session.save(dev_wc);
		session.save(dev_lz);
		
		session.save(prj_ds);
		session.save(prj_oa);
		
		session.getTransaction().commit();
		session.close();
		//得到九条结果
		/*Hibernate: insert into t_developer (d_name) values (?)
		Hibernate: insert into t_developer (d_name) values (?)
		Hibernate: insert into t_developer (d_name) values (?)
		Hibernate: insert into t_project (prj_name) values (?)
		Hibernate: insert into t_project (prj_name) values (?)
		Hibernate: insert into t_relation (prjId, did) values (?, ?)
		Hibernate: insert into t_relation (prjId, did) values (?, ?)
		Hibernate: insert into t_relation (prjId, did) values (?, ?)
		Hibernate: insert into t_relation (prjId, did) values (?, ?)
	*/
		
	}
	
	@Test
	public void Save2() {
		Session session=sf.openSession();
		session.beginTransaction();
		
	
		session.getTransaction().commit();
		session.close();
	
		
		
	}
}

 

你可能感兴趣的:(hibernate,hibernate)