多对多映射关系:
开发步骤:
JavaBean 映射文件 测试
图片详解:
1.javaBean
Project.java
package cn.itcast.many2many; import java.util.HashSet; import java.util.Set; public class Project { private int proj_id; @Override public String toString() { return "Project [proj_id=" + proj_id + ", proj_name=" + proj_name + ", developers=" + developers + "]"; } private String proj_name; private Set<Developer> developers=new HashSet<Developer>(); public int getProj_id() { return proj_id; } public void setProj_id(int proj_id) { this.proj_id = proj_id; } public String getProj_name() { return proj_name; } public void setProj_name(String proj_name) { this.proj_name = proj_name; } public Set<Developer> getDevelopers() { return developers; } public void setDevelopers(Set<Developer> developers) { this.developers = developers; } }
Employee.java
package cn.itcast.many2many; import java.util.HashSet; import java.util.Set; public class Developer { @Override public String toString() { return "Developer [d_id=" + d_id + ", d_name=" + d_name + ", projects=" + projects + "]"; } private int d_id; private String d_name; private Set<Project> projects=new HashSet<Project>(); public int getD_id() { return d_id; } public void setD_id(int 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<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
2.配置文件
多对多的关系映射
1.映射的集合属性
2.属性对应的中间表
3.外键字段 projid
4.外键字段对应的中间表属性did
5.集合属性元素的类型
Project.hbm.xml
<?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.itcast.many2many" auto-import="true"> <class name="Project"> <id name="proj_id"> <generator class="native"></generator> </id> <property name="proj_name" column="proj_name" length="20" type="string"></property> <!-- 多对多的关系映射 1.映射的集合属性 2.属性对应的中间表 3.外键字段 projid 4.外键字段对应的中间表属性did 5.集合属性元素的类型 --> <set name="developers" table="t_relation" cascade="save-update" inverse="true"> <key column="projid"></key> <many-to-many class="Developer" column="did"></many-to-many> </set> </class> </hibernate-mapping>
Developer.hbm.xml
<?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.itcast.many2many" auto-import="true"> <class name="Developer"> <id name="d_id"> <generator class="native"></generator> </id> <property name="d_name" column="d_name" length="20" type="string"></property> <!-- 这里配置的是多对多的关系映射 --> <set name="projects" table="t_relation"> <key column="did"></key> <many-to-many class="Project" column="projid"></many-to-many> </set> </class> </hibernate-mapping>
测试类:
package cn.itcast.many2many; import org.hibernate.Transaction; import org.hibernate.classic.Session; import org.junit.Test; import cn.itcast.utils.HibernateUtil; public class App { @Test public void test(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); //通过项目表维护员工表 //1.项目表 Project proj=new Project(); proj.setProj_name("OA项目"); Project proj2=new Project(); proj2.setProj_name("ERP系统"); //2.员工表 Developer dev_lwk=new Developer(); dev_lwk.setD_name("李卫康"); Developer dev_hw=new Developer(); dev_hw.setD_name("韩玮"); Developer dev_ccs=new Developer(); dev_ccs.setD_name("程崇树"); //3.关系 proj.getDevelopers().add(dev_lwk); proj.getDevelopers().add(dev_hw); proj2.getDevelopers().add(dev_lwk); proj2.getDevelopers().add(dev_ccs); session.save(proj); session.save(proj2); transaction.commit(); session.close(); } }
运行测试得到的数据库表:
t_relation的外键
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 02:32:07