Role类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.cnblogs.hibernate_first; import java.util.Set; public class Role { private int id; private String name; private Set users; public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Role() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
User类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.cnblogs.hibernate_first; import java.util.Set; public class User { private int id; private String name; private Set roles; public User() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getRoles() { return roles; } public void setRoles(Set roles) { this.roles = roles; } }
Role.hbm.xml:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="Role" table="t_role"> <id name="id" column="t_id"> <generator class="native"></generator> </id> <property name="name" column="t_name" /> <set name="users" table="t_user_role"> <key column="role_id"></key> <many-to-many class="User" column="user_id"></many-to-many> </set> </class> </hibernate-mapping>
User.hbm.xml:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="User" table="t_user"> <id name="id" column="t_id"> <generator class="native"></generator> </id> <property name="name" column="t_name" /> <set name="roles" table="t_user_role"> <key column="user_id"></key> <many-to-many class="com.cnblogs.hibernate_first.Role" column="role_id"></many-to-many> </set> </class> </hibernate-mapping>
测试类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.cnblogs.hibernate_first; import java.util.HashSet; import java.util.Set; import javax.persistence.EntityTransaction; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import junit.framework.TestCase; public class many2manyTest extends TestCase { public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Role role1 = new Role(); role1.setName("数据录入人员"); session.save(role1); Role role2 = new Role(); role2.setName("商务主管"); session.save(role2); Role role3 = new Role(); role3.setName("商务经理"); session.save(role3); Role role4 = new Role(); role4.setName("项目会计"); session.save(role4); User user1 = new User(); user1.setName("张三"); Set roles = new HashSet<>(); roles.add(role1); roles.add(role2); user1.setRoles(roles); session.save(user1); // 错误示例 // User user2 = new User(); // user2.setName("李四"); // user2.getRoles().add(role1); // 如果这么写的话,这里会报空指针异常。因为getRoles()方法获得不到set // user2.getRoles().add(role2); // user2.getRoles().add(role3); // session.save(user2); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } //从user方向加载查询职责 public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user1 = session.load(User.class, 1); System.out.println("user1 的姓名是 :" + user1.getName() + "他的职责是 :" + user1.getRoles().toString()); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } //从职责方向查询人员 public void testLoad2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Role role1 = session.load(Role.class, 2); System.out.println("role1 职责是:" + role1.getName()); System.out.println("role1对应的人员是" + role1.getUsers()); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } }