标签:hibernate orm 多对多关联映射 映射文件 框架
多对多可通过两个多对一实现(把第三张表抽取出一个实体,然后两个多对应一个新实体)
多对多会建立三张表,第三张表存放两个外键分别指向两个多;然后两个外键作为第三张表的联合主键
一、单向关联映射
Many(维护端:User)
<?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="org.app.model"> <class name="User"table="t_user"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> <!-- 维护关系 --> <!-- table:指定第三张表的表名 --> <set name="roles"table="t_user_role"> <!—在‘多’的一端(第三张表)添加外键(user_id)指本端 --> <key column="user_id"></key> <!—- 在多的一端(第三张表)添加外键(role_id)指向关系端 --> <many-to-many class="org.app.model.Role" column="role_id" /> </set> </class> </hibernate-mapping>
Many(非维护端:User)
<?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="org.app.model"> <class name="Role" table="t_role"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> </class> </hibernate-mapping>
public void testManyToMany() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); // 先保存非维护端 Role role1 = new Role(); role1.setName("role1"); session.save(role1); Role role2 = new Role(); role2.setName("role2"); session.save(role2); Role role3 = new Role(); role3.setName("role3"); session.save(role3); Role role4 = new Role(); role4.setName("role4"); session.save(role4); // 再保存维护端 User u1 = new User(); u1.setName("name1"); Set<Role> u1Roles = new HashSet<Role>(); u1Roles.add(role1); u1Roles.add(role2); u1.setRoles(u1Roles); session.save(u1); User u2 = new User(); u2.setName("name2"); Set<Role> u2Roles = new HashSet<Role>(); u2Roles.add(role1); u2Roles.add(role2); u2Roles.add(role3); u2.setRoles(u2Roles); session.save(u2); User u3 = new User(); u3.setName("u3"); Set<Role> u3Roles = new HashSet<Role>(); u3Roles.add(role3); u3Roles.add(role4); u3.setRoles(u3Roles); session.save(u3); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } }
二、双向关联映射
可在任意一端使用reverse属性反转让只让另一端作为维护端
Many(维护端:User)
<?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="org.app.model"> <class name="User" table="t_user"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> <set name="roles"table="t_user_role"> <key column="user_id"></key> <many-to-many class="org.app.model.Role" column="role_id" /> </set> </class> </hibernate-mapping>
Many(维护端:User)
<?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="org.app.model"> <class name="Role" table="t_role"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> <set name="users"table="t_user_role"> <!—在‘多’的一端(第三张表)添加外键(role_id)指向本端--> <key column="role_id"/> <!—维护关系:在‘多’的一端(第三张表)添加外键(user_id)指向关系端--> <many-to-many class="org.app.model.User" column="user_id" /> </set> </class> </hibernate-mapping>
标签:hibernate orm 多对多关联映射 映射文件 框架
原文地址:http://blog.csdn.net/hekewangzi/article/details/41519261