<?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="cn.manytomany.doubleanother">
<class table="PROEMPNEW">
<id>
<generator>
<param>SQU_NUM</param>
</generator>
</id>
<many-to-one column="EMPID">
</many-to-one>
<many-to-one column="PROID">
</many-to-one>
</class>
</hibernate-mapping>
3. 现在就可以进行测试类测试数据了
package cn.manytomany.doubleanother;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.manytomany.one.HibernateUtil;
public class ManyToManyDoubleOnlyAnother {
/**
* 多对多双向关联---两个多对一关联
*/
public static void main(String[] args) {
Session session = HibernateUtil.currentSession();
Transaction tsc = session.beginTransaction();
//创建雇员
Emploee emp=new Emploee();
emp.setEmpName("田超");
//创建工程
Project pro=new Project();
pro.setProName("开发工程");
//中间类
ProEmp proemp=new ProEmp();
proemp.setEmp(emp);
proemp.setPro(pro);
try {
//保存
session.save(emp);
session.save(pro);
session.save(proemp);
tsc.commit();
} catch (Exception e) {
// 回滚
tsc.rollback();
}
HibernateUtil.closeSession();
}
}
好了, Hibernate的多对多双向关联的两种方案已经完成,如果觉得对你们有用的话,记得点个关注啊!!!