Hibernate中双向多对多的两种配置方式
Hibernate中双向多对多的两种配置方式
1.建立多对多双向关联关系
1 package cn.happy.entitys; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Employee { 7 private Integer empid; 8 private String empname; 9 private Set<ProEmp> emps = new HashSet<ProEmp>(); 10 11 public Set<ProEmp> getEmps() { 12 return emps; 13 } 14 15 public void setEmps(Set<ProEmp> emps) { 16 this.emps = emps; 17 } 18 19 public Integer getEmpid() { 20 return empid; 21 } 22 23 public void setEmpid(Integer empid) { 24 this.empid = empid; 25 } 26 27 public String getEmpname() { 28 return empname; 29 } 30 31 public void setEmpname(String empname) { 32 this.empname = empname; 33 } 34 35 public Set<ProEmp> getProjects() { 36 return projects; 37 } 38 39 public void setProjects(Set<ProEmp> projects) { 40 this.projects = projects; 41 } 42 43 private Set<ProEmp> projects = new HashSet<ProEmp>(); 44 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entitys"> 6 7 <class name="Employee" table="EMPLOYEE"> 8 <id name="empid" column="EMPID"> 9 <generator class="sequence"> 10 <param name="sequence">STU_SID</param> 11 </generator> 12 </id> 13 <property name="empname" type="string" column="EMPNAME"></property> 14 15 <set name="projects" table="PROEMP" cascade="save-update"> 16 <key column="REMPID"></key> 17 <many-to-many class="Project" column="RPROID"></many-to-many> 18 </set> 19 20 </class> 21 22 </hibernate-mapping>
1 package cn.happy.entitys; 2 3 import java.util.Set; 4 5 public class Project { 6 private Integer proid; 7 private String proname; 8 private Set<ProEmp> pros; 9 public Integer getProid() { 10 return proid; 11 } 12 public Set<ProEmp> getPros() { 13 return pros; 14 } 15 public void setPros(Set<ProEmp> pros) { 16 this.pros = pros; 17 } 18 public void setProid(Integer proid) { 19 this.proid = proid; 20 } 21 public String getProname() { 22 return proname; 23 } 24 public void setProname(String proname) { 25 this.proname = proname; 26 } 27 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entity"> 6 7 <class name="Project" table="PROJECT"> 8 <id name="proid" column="PROID"> 9 <generator class="sequence"> 10 <param name="sequence">STU_SID</param> 11 </generator> 12 </id> 13 <property name="proname" type="string" column="PRONAME"></property> 14 <set name="employeses" table="PROEMP" cascade="save-update"> 15 <key column="RPROID"></key> 16 <many-to-many class="Employee" column="REMPID"></many-to-many> 17 </set> 18 </class> 19 20 </hibernate-mapping>
中间关系表
1 package cn.happy.entitys; 2 3 public class ProEmp { 4 private Integer id; 5 private Project pro; 6 private Employee emp; 7 8 public Integer getId() { 9 return id; 10 } 11 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 16 public Project getPro() { 17 return pro; 18 } 19 20 public void setPro(Project pro) { 21 this.pro = pro; 22 } 23 24 public Employee getEmp() { 25 return emp; 26 } 27 28 public void setEmp(Employee emp) { 29 this.emp = emp; 30 } 31 }
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 <!-- Database connection settings --> 10 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 11 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 12 <property name="connection.username">hmy</property> 13 <property name="connection.password">hmy</property> 14 15 <!-- SQL dialect SQL方言--> 16 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 17 18 <!-- Echo all executed SQL to stdout 控制台显示--> 19 <property name="show_sql">true</property> 20 <!-- Enable Hibernate's automatic session context management --> 21 <property name="current_session_context_class">thread</property> 22 <!-- 是否格式化 --> 23 <property name="format_sql">true</property> 24 25 <!-- Drop and re-create the database schema on startup --> 26 <property name="hbm2ddl.auto">create</property> 27 28 <!-- <mapping resource="cn/happy/entity/Student.hbm.xml" /> --> 29 <!-- <mapping resource="cn/happy/entity/grade.hbm.xml" /> --> 30 <!-- <mapping class="cn.happy.entity.Grade"/> --> 31 <!-- <mapping resource="cn/happy/entity/Dept.hbm.xml" /> 32 <mapping resource="cn/happy/entity/Emp.hbm.xml" /> --> 33 <mapping resource="cn/happy/entity/Employee.hbm.xml" /> 34 <mapping resource="cn/happy/entity/Project.hbm.xml" /> 35 36 <!-- <mapping resource="cn/happy/entitys/Employee.hbm.xml" /> 37 <mapping resource="cn/happy/entitys/Project.hbm.xml" /> 38 <mapping resource="cn/happy/entitys/ProEmp.hbm.xml" /> --> 39 40 </session-factory> 41 42 </hibernate-configuration>
1 package cn.happy.ui; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import cn.happy.entity.Employee; 7 import cn.happy.entity.Project; 8 import cn.happy.util.HibernateUtil; 9 10 public class Test3 { 11 public static void main(String[] args) { 12 select(); 13 } 14 15 /** 16 * 检索 17 * */ 18 public static void select(){ 19 Session session = HibernateUtil.currentSession(); 20 Project pro = (Project)session.load(Project.class, 1); 21 for (Employee item : pro.getEmployeses()) { 22 System.out.println("员工:"+item.getEmpname()+"\t参加的项目:"); 23 for (Project project : item.getProjects()) { 24 System.out.println(project.getProname()); 25 } 26 } 27 HibernateUtil.closeSession(); 28 } 29 30 /** 31 * 添加 32 * */ 33 public static void one(){ 34 Session session = HibernateUtil.currentSession(); 35 Transaction tx = session.beginTransaction(); 36 37 Project pro=new Project(); 38 pro.setProname("ADO"); 39 40 Employee emp=new Employee(); 41 emp.setEmpname("范子阳"); 42 Employee emp2=new Employee(); 43 emp2.setEmpname("李夏鹏"); 44 45 pro.getEmployeses().add(emp); 46 pro.getEmployeses().add(emp2); 47 48 //未声明级联 49 //emp.getProjects().add(pro); 50 //emp2.getProjects().add(pro); 51 52 session.save(pro); 53 54 tx.commit(); 55 HibernateUtil.closeSession(); 56 } 57 }
2.建立多对多双向关联 拆成两个一对多
1 package cn.happy.entitys; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Employee { 7 private Integer empid; 8 private String empname; 9 private Set<ProEmp> emps = new HashSet<ProEmp>(); 10 11 public Set<ProEmp> getEmps() { 12 return emps; 13 } 14 15 public void setEmps(Set<ProEmp> emps) { 16 this.emps = emps; 17 } 18 19 public Integer getEmpid() { 20 return empid; 21 } 22 23 public void setEmpid(Integer empid) { 24 this.empid = empid; 25 } 26 27 public String getEmpname() { 28 return empname; 29 } 30 31 public void setEmpname(String empname) { 32 this.empname = empname; 33 } 34 35 public Set<ProEmp> getProjects() { 36 return projects; 37 } 38 39 public void setProjects(Set<ProEmp> projects) { 40 this.projects = projects; 41 } 42 43 private Set<ProEmp> projects = new HashSet<ProEmp>(); 44 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entitys"> 6 7 <class name="Employee" table="EMPLOYEE"> 8 <id name="empid" column="EMPID"> 9 <generator class="sequence"> 10 <param name="sequence">STU_SID</param> 11 </generator> 12 </id> 13 14 15 </class> 16 17 </hibernate-mapping>
1 package cn.happy.entitys; 2 3 import java.util.Set; 4 5 public class Project { 6 private Integer proid; 7 private String proname; 8 private Set<ProEmp> pros;//为中间关系表所对应的实体类的类型 9 public Integer getProid() { 10 return proid; 11 } 12 public Set<ProEmp> getPros() { 13 return pros; 14 } 15 public void setPros(Set<ProEmp> pros) { 16 this.pros = pros; 17 } 18 public void setProid(Integer proid) { 19 this.proid = proid; 20 } 21 public String getProname() { 22 return proname; 23 } 24 public void setProname(String proname) { 25 this.proname = proname; 26 } 27 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entitys"> 6 7 <class name="Project" table="PROJECT"> 8 <id name="proid" column="PROID"> 9 <generator class="sequence"> 10 <param name="sequence">STU_SID</param> 11 </generator> 12 </id> 13 <property name="proname" type="string" column="PRONAME"></property> 14 15 </class> 16 17 </hibernate-mapping>
1 package cn.happy.entitys; 2 3 public class ProEmp { 4 private Integer id; 5 private Project pro; 6 private Employee emp; 7 8 public Integer getId() { 9 return id; 10 } 11 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 16 public Project getPro() { 17 return pro; 18 } 19 20 public void setPro(Project pro) { 21 this.pro = pro; 22 } 23 24 public Employee getEmp() { 25 return emp; 26 } 27 28 public void setEmp(Employee emp) { 29 this.emp = emp; 30 } 31 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entitys"> 6 7 <class name="ProEmp" table="PROEMP"> 8 <id name="id" column="ID"> 9 <generator class="sequence"> 10 <param name="sequence">STU_SID</param> 11 </generator> 12 </id> 13 14 <many-to-one name="emp" class="Employee" column="empid"></many-to-one> 15 <many-to-one name="pro" class="Project" column="proid"></many-to-one> 16 </class> 17 18 </hibernate-mapping>
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 <!-- Database connection settings --> 10 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 11 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 12 <property name="connection.username">hmy</property> 13 <property name="connection.password">hmy</property> 14 15 <!-- SQL dialect SQL方言--> 16 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 17 18 <!-- Echo all executed SQL to stdout 控制台显示--> 19 <property name="show_sql">true</property> 20 <!-- Enable Hibernate's automatic session context management --> 21 <property name="current_session_context_class">thread</property> 22 <!-- 是否格式化 --> 23 <property name="format_sql">true</property> 24 25 <!-- Drop and re-create the database schema on startup --> 26 <property name="hbm2ddl.auto">create</property> 27 28 <!-- <mapping resource="cn/happy/entity/Student.hbm.xml" /> --> 29 <!-- <mapping resource="cn/happy/entity/grade.hbm.xml" /> --> 30 <!-- <mapping class="cn.happy.entity.Grade"/> --> 31 <!-- <mapping resource="cn/happy/entity/Dept.hbm.xml" /> 32 <mapping resource="cn/happy/entity/Emp.hbm.xml" /> --> 33 <!-- <mapping resource="cn/happy/entity/Employee.hbm.xml" /> 34 <mapping resource="cn/happy/entity/Project.hbm.xml" /> --> 35 36 <mapping resource="cn/happy/entitys/Employee.hbm.xml" /> 37 <mapping resource="cn/happy/entitys/Project.hbm.xml" /> 38 <mapping resource="cn/happy/entitys/ProEmp.hbm.xml" /> 39 40 </session-factory> 41 42 </hibernate-configuration>
1 package cn.happy.ui; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import cn.happy.entitys.Employee; 7 import cn.happy.entitys.ProEmp; 8 import cn.happy.entitys.Project; 9 import cn.happy.util.HibernateUtil; 10 11 public class Test4 { 12 public static void main(String[] args) { 13 test(); 14 } 15 public static void test(){ 16 Session session = HibernateUtil.currentSession(); 17 Transaction tx = session.beginTransaction(); 18 19 Project pro=new Project(); 20 pro.setProname("ADO"); 21 22 Employee emp=new Employee(); 23 emp.setEmpname("范子阳"); 24 25 ProEmp proemp=new ProEmp(); 26 proemp.setEmp(emp); 27 proemp.setPro(pro); 28 29 session.save(proemp); 30 session.save(emp); 31 session.save(pro); 32 33 tx.commit(); 34 HibernateUtil.closeSession(); 35 } 36 37 }