Role类
package com.cnblogs.hibernate_first; public class Role { private int id; private String name; 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类
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; } }
user.hbm.xml
<?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>
role.hbm.xml
<?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" /> </class> </hibernate-mapping>
test 类
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); } } public void testLoad2() { 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); } } }