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