package loaderman.c_many2many; import java.util.HashSet; import java.util.Set; /** * 开发人员 * * */ public class Developer { 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 dId) { d_id = dId; } public String getD_name() { return d_name; } public void setD_name(String dName) { d_name = dName; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
package loaderman.c_many2many; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App1_save { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Project.class) .addClass(Developer.class) // 测试时候使用 .buildSessionFactory(); } // 1. 多对多,保存 【只能通过一方维护另外一方,不能重复维护!】 @Test public void save() { Session session = sf.openSession(); session.beginTransaction(); /* * 模拟数据: 电商系统(曹吉,王春) OA系统(王春,老张) */ // 创建项目对象 Project prj_ds = new Project(); prj_ds.setPrj_name("电商系统"); Project prj_oa = new Project(); prj_oa.setPrj_name("OA系统"); // 创建员工对象 Developer dev_cj = new Developer(); dev_cj.setD_name("曹吉"); Developer dev_wc = new Developer(); dev_wc.setD_name("王春"); Developer dev_lz = new Developer(); dev_lz.setD_name("老张"); // 关系 【项目方】 prj_ds.getDevelopers().add(dev_cj); prj_ds.getDevelopers().add(dev_wc); // 电商系统(曹吉,王春) prj_oa.getDevelopers().add(dev_wc); prj_oa.getDevelopers().add(dev_lz); // OA系统(王春,老张) // 保存 // session.save(dev_cj); // session.save(dev_wc); // session.save(dev_lz); session.save(prj_ds); session.save(prj_oa); // 必须要设置级联保存 session.getTransaction().commit(); session.close(); } @Test public void bak() { Session session = sf.openSession(); session.beginTransaction(); session.getTransaction().commit(); session.close(); } }
package loaderman.c_many2many; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; public class App2_inverse { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(Project.class) .addClass(Developer.class) // 测试时候使用 .buildSessionFactory(); } // 多对多 //1. 设置inverse属性,对保存数据影响? // 有影响。 // inverse=false ,有控制权,可以维护关联关系; 保存数据的时候会把对象关系插入中间表; // inverse=true, 没有控制权, 不会往中间表插入数据。 @Test public void save() { Session session = sf.openSession(); session.beginTransaction(); /* * 模拟数据: 电商系统(曹吉,王春) OA系统(王春,老张) */ // 创建项目对象 Project prj_ds = new Project(); prj_ds.setPrj_name("电商系统"); Project prj_oa = new Project(); prj_oa.setPrj_name("OA系统"); // 创建员工对象 Developer dev_cj = new Developer(); dev_cj.setD_name("曹吉"); Developer dev_wc = new Developer(); dev_wc.setD_name("王春"); Developer dev_lz = new Developer(); dev_lz.setD_name("老张"); // 关系 【项目方】 prj_ds.getDevelopers().add(dev_cj); prj_ds.getDevelopers().add(dev_wc); // 电商系统(曹吉,王春) prj_oa.getDevelopers().add(dev_wc); prj_oa.getDevelopers().add(dev_lz); // OA系统(王春,老张) // 保存 // session.save(dev_cj); // session.save(dev_wc); // session.save(dev_lz); session.save(prj_ds); session.save(prj_oa); // 必须要设置级联保存 session.getTransaction().commit(); session.close(); } //2 .设置inverse属性, 对获取数据影响? 无 @Test public void get() { Session session = sf.openSession(); session.beginTransaction(); Project prj = (Project) session.get(Project.class, 1); System.out.println(prj.getPrj_name()); System.out.println(prj.getDevelopers()); session.getTransaction().commit(); session.close(); } //3. 设置inverse属性, 对解除关系影响? // 有影响。 // inverse=false ,有控制权, 解除关系就是删除中间表的数据。 // inverse=true, 没有控制权,不能解除关系。 @Test public void removeRelation() { Session session = sf.openSession(); session.beginTransaction(); Project prj = (Project) session.get(Project.class, 7); prj.getDevelopers().clear(); session.getTransaction().commit(); session.close(); } //3. 设置inverse属性,对删除数据的影响? // inverse=false, 有控制权。 先删除中间表数据,再删除自身。 // inverse=true, 没有控制权。 如果删除的数据有被引用,会报错! 否则,才可以删除 @Test public void deleteData() { Session session = sf.openSession(); session.beginTransaction(); Project prj = (Project) session.get(Project.class, 1); session.delete(prj); session.getTransaction().commit(); session.close(); } }
<?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="loaderman.c_many2many"> <class name="Developer" table="t_developer"> <id name="d_id"> <generator class="native"></generator> </id> <property name="d_name" length="20"></property> <!-- 多对多映射配置: 员工方 name 指定映射的集合属性 table 集合属性对应的中间表 key 指定中间表的外键字段(引用当前表t_developer主键的外键字段) many-to-many column 指定外键字段对应的项目字段 class 集合元素的类型 --> <set name="projects" table="t_relation"> <key column="did"></key> <many-to-many column="prjId" class="Project"></many-to-many> </set> </class> </hibernate-mapping>
<?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="loaderman.c_many2many"> <class name="Project" table="t_project"> <id name="prj_id"> <generator class="native"></generator> </id> <property name="prj_name" length="20"></property> <!-- 多对多映射: 1. 映射的集合属性: “developers” 2. 集合属性,对应的中间表: “t_relation” 3. 外键字段: prjId 4. 外键字段,对应的中间表字段: did 5. 集合属性元素的类型 --> <set name="developers" table="t_relation" cascade="save-update" inverse="true"> <key column="prjId"></key> <many-to-many column="did" class="Developer"></many-to-many> </set> </class> </hibernate-mapping>
package loaderman.c_many2many; import java.util.HashSet; import java.util.Set; /** * 项目 * * @author Jie.Yuan * */ public class Project { private int prj_id; private String prj_name; // 项目下的多个员工 private Set<Developer> developers = new HashSet<Developer>(); public int getPrj_id() { return prj_id; } public void setPrj_id(int prjId) { prj_id = prjId; } public String getPrj_name() { return prj_name; } public void setPrj_name(String prjName) { prj_name = prjName; } public Set<Developer> getDevelopers() { return developers; } public void setDevelopers(Set<Developer> developers) { this.developers = developers; } }