多对多映射
需求:一个项目由多个员工开发,一个员工开发多个项目
设计数据库表
一般地,如果是多对多的映射,我们都会使用一张中间表来保存它们的关联关系….
这里写图片描述
设计实体
我们在设计实体的时候,一般是核心数据表对应一个JavaBean实体【中间表并不是核心数据表】,那么我们将会设计两个JavaBean对象
这里写图片描述
project.java
package zhongfucheng.many2many; import java.util.HashSet; import java.util.Set; /** * Created by ozc on 2017/5/7. */ public class Project { private int projectId; private String projectName; //使用Set集合与developer实体维护关系 private Set<Developer> developers = new HashSet<>(); public int getProjectId() { return projectId; } public void setProjectId(int projectId) { this.projectId = projectId; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } public Set<Developer> getDevelopers() { return developers; } public void setDevelopers(Set<Developer> developers) { this.developers = developers; } }
developer.java
package zhongfucheng.many2many; import java.util.HashSet; import java.util.Set; /** * Created by ozc on 2017/5/7. */ public class Developer { private int developerId; private String developerName; //使用Set集合来维护与Project关系 private Set<Project> projects = new HashSet<>(); public int getDeveloperId() { return developerId; } public void setDeveloperId(int developerId) { this.developerId = developerId; } public String getDeveloperName() { return developerName; } public void setDeveloperName(String developerName) { this.developerName = developerName; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
映射配置文件
以项目映射文件为例:我们不急着写,首先来分析一下关键点……想要在多对多映射中产生正确的关联关系,下面几步必不可少:
-
配置映射集合的属性(developers)
-
映射集合对应的中间表(developer_project)
-
中间表的外键字段(project_id)
-
集合元素的类型(Developer)
- 中间表另外的外键字段(developer_id)
Project和Developer的映射文件都需要这几个关键步骤
Project映射文件
<?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"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.many2many"> <class name="Project" table="Project"> <!--映射主键--> <id name="projectId" column="projectId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="projectName" column="projectName"></property> <!--映射多对多的关系--> <!--Set的属性名称为developers,对应developer_project表--> <set name="developers" table="developer_project"> <!--对应developer_project表的外键列--> <key column="project_id"></key> <!--集合的类型和developer_project表的另一个外键列--> <many-to-many column="developer_id" class="Developer"></many-to-many> </set> </class> </hibernate-mapping>
Developer映射文件
<?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"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.many2many"> <class name="Developer" table="Developer"> <!--映射主键--> <id name="developerId" column="developerId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="developerName" column="developerName"></property> <!--映射多对多的关系--> <!--Set的属性名称为developers,对应developer_project表--> <set name="projects" table="developer_project"> <!--对应developer_project表的外键列--> <key column="developer_id"></key> <!--集合的类型和developer_project表的另一个外键列--> <many-to-many column="project_id" class="Project"></many-to-many> </set> </class> </hibernate-mapping>
测试
package zhongfucheng.many2many; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App2 { public static void main(String[] args) { /* * * * Project Developer 电商系统 曹吉 王春 OA系统 王春 老张 */ //创建对象 Developer cj = new Developer(); Developer wc = new Developer(); Developer lz = new Developer(); Project ds = new Project(); Project oa = new Project(); //设置对象的数据 cj.setDeveloperName("曹吉"); wc.setDeveloperName("王春"); lz.setDeveloperName("老张"); oa.setProjectName("OA系统"); ds.setProjectName("电商系统"); //使用Project来关联数据【在多对多中,一样的】 oa.getDevelopers().add(wc); oa.getDevelopers().add(lz); ds.getDevelopers().add(cj); ds.getDevelopers().add(wc); //获取加载配置管理类 Configuration configuration = new Configuration(); //加载User的映射文件! configuration.configure().addClass(Developer.class).addClass(Project.class); //创建Session工厂对象 SessionFactory factory = configuration.buildSessionFactory(); //得到Session对象 Session session = factory.openSession(); //使用Hibernate操作数据库,都要开启事务,得到事务对象 Transaction transaction = session.getTransaction(); //开启事务 transaction.begin(); //在Project映射文件中设置级联保存了 session.save(oa); session.save(ds); //提交事务 transaction.commit(); //关闭Session session.close(); } }
执行了9条SQL语句,数据库中的记录也是正确的。
这里写图片描述