实体多对多

工具eclipse sqlserver2000

1.建立web工程bank 

  在src下建立包third third.test

在包third下建Employee.java Project.java 以及映射文件Employee.hbm.xml

Project.hbm.xml

在包third.test包下写测试类Demo.java

在工程下引入hibernate所要用的jar包,以及sqlserver驱动的jar包

2.Employee.java

package third;

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

public class Employee {
	private int employee_id;
	private String employee_name;
	private String employee_descr;
	private Set projs=new HashSet();	
	public Employee() {
		super();
	}
	public int getEmployee_id() {
		return employee_id;
	}
	public void setEmployee_id(int employee_id) {
		this.employee_id = employee_id;
	}
	public String getEmployee_name() {
		return employee_name;
	}
	public void setEmployee_name(String employee_name) {
		this.employee_name = employee_name;
	}
	public String getEmployee_descr() {
		return employee_descr;
	}
	public void setEmployee_descr(String employee_descr) {
		this.employee_descr = employee_descr;
	}
	public Set getProjs() {
		return projs;
	}
	public void setProjs(Set projs) {
		this.projs = projs;
	}
	

}

 

Employee.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="third">
	<class name="Employee" table="tbl_emp">
	<id name="employee_id" >
		<generator class="assigned" />
	</id>
	
	<property name="employee_name" >
	</property>
	
	<property name="employee_descr" >
	</property>
<!-- 本类属性projs,与表 tbl_proj多对多关系,需要经过中间过渡表r_emp_proj-->
<set name="projs" table="r_emp_proj" inverse="true">
<key column="r_emp_id"/>
<many-to-many class="Project" column="r_proj_id"/>
</set>
  
	</class>
	</hibernate-mapping>
			

 

3.Project.java

package third;

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

public class Project {
	private int project_id;
	private String project_name;
	private String project_descr;
	private Set emps=new HashSet();
	public Project() {
		super();
	}	
	public Set getEmps() {
		return emps;
	}
	public void setEmps(Set emps) {
		this.emps = emps;
	}

	public int getProject_id() {
		return project_id;
	}
	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}
	public String getProject_name() {
		return project_name;
	}
	public void setProject_name(String project_name) {
		this.project_name = project_name;
	}
	public String getProject_descr() {
		return project_descr;
	}
	public void setProject_descr(String project_descr) {
		this.project_descr = project_descr;
	}
	

}

 

Project.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="third">
	<class name="Project" table="tbl_proj">
	<id name="project_id" >
		<generator class="native" />
	</id>
	
	<property name="project_name" >
	</property>
     
     <property name="project_descr" >
	</property>
	<!-- 本类属性emps与对象Employee是多对多关系,需要经过中间过渡表r_emp_proj -->
    <!--  -->
   <set name="emps" table="r_emp_proj">
   <key column="r_proj_id" />
   <many-to-many  class="Employee"  column="r_emp_id"/>
   </set>
   
	</class>
	</hibernate-mapping>
	
	
	

 4.Demo.java

package third.test;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import third.Employee;
import third.Project;

public class Demo {
@Test
public void tt(){
	Configuration cfg=new Configuration();
	cfg.configure();
	SessionFactory se=cfg.buildSessionFactory();
	Session s=se.openSession();
	Transaction tr=s.beginTransaction();
	try{
		Project p=new Project();
		Employee em=new Employee();
		
		p.setProject_name("智慧供水平台");
		p.setProject_descr("集成所有供水业务");
		
		Set pros=new HashSet();
	    pros.add(p);
	    
		em.setEmployee_id(02);
		em.setEmployee_name("飞鸟");
		em.setEmployee_descr("开发");
		
	    Set ems=new HashSet();
	    ems.add(em);
	    p.setEmps(ems);
	    em.setProjs(pros);
	    
	    s.save(p);
	    s.save(em);
	    tr.commit();	
	}catch(Exception e){
		if(tr!=null){
			tr.rollback();
		}
	}
	finally{
		s.close();
	}
	
	
}
}

 

你可能感兴趣的:(多对多)