首页 > 代码库 > hibernate之 10.many2many单向
hibernate之 10.many2many单向
在前文hibernate之5.many2one单向 提到多对多关系,表结构设计是基于中间表来实现,
下面以用户与角色(多对多)为例,在Hibernate是如何操作的
表结构设计:
类图:
CRUD;
Student
package com.demo.model; import java.io.UnsupportedEncodingException; import java.util.Set; /**学生信息 * @author wobendiankun *2014-10-19 下午08:54:29 */ public class Student { private int studentId ; private String studentName ; private int age; private Set<Certificate> certificates ; private User user; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { String str=""; if(studentName!=null){ try { str=new String(studentName.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return "Student [studentId=" + studentId + ", studentName=" + str + ", age=" + age + "]"; } public Set<Certificate> getCertificates() { return certificates; } public void setCertificates(Set<Certificate> certificates) { this.certificates = certificates; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }Role
package com.demo.model; import java.util.Set; /**角色信息 * @author wobendiankun *2014-11-2 下午08:29:54 */ public class Role { /** * 角色id */ private int roleId; /** * 角色名称 */ private String roleName; public Role() { } public Role(int roleId) { this.roleId = roleId; } public Role(String roleName) { this.roleName = roleName; } private Set<User> users; public int getRoleId() { return roleId; } public void setRoleId(int roleId) { this.roleId = roleId; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }配置:
Role.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="com.demo.model.Role" table="t_role"> <id name="roleId" column="role_id"> <generator class="sequence"> <param name="sequence">SEQ_T_ROLE</param> </generator> </id> <property name="roleName" column="role_name" /> <set name="users" table="t_user_role" lazy="extra"> <key column="role_id"></key> <many-to-many class="com.demo.model.User" column="user_id"></many-to-many> </set> </class> </hibernate-mapping>
<set name="users" table="t_user_role" lazy="extra">
<key column="role_id"></key>
<many-to-many class="com.demo.model.User" column="user_id"></many-to-many>
</set>set元素的table属性::指中间表名
key元素的column属性:指表t_role在中间表中的关联字段
many-to-many元素的class属性:指set中的元素类型 ,column:User实体映射表t_user在中间表的关联字段
add:
</pre><pre name="code" class="java">@Test <span style="white-space:pre"> </span>public void addTest() { <span style="white-space:pre"> </span>User user1=new User("李三","111"); <span style="white-space:pre"> </span>User user2=new User("李四","111"); <span style="white-space:pre"> </span>Set<User> users=new HashSet<User>(); <span style="white-space:pre"> </span>users.add(user1); <span style="white-space:pre"> </span>users.add(user2); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>Role role=new Role("开发组"); <span style="white-space:pre"> </span>role.setUsers(users); <span style="white-space:pre"> </span>Session session = null; <span style="white-space:pre"> </span>try { <span style="white-space:pre"> </span>session = HibernateUtil.openSession(); <span style="white-space:pre"> </span>session.beginTransaction(); <span style="white-space:pre"> </span>session.save(user1); <span style="white-space:pre"> </span>session.save(user2); <span style="white-space:pre"> </span>session.save(role); <span style="white-space:pre"> </span>session.getTransaction().commit(); <span style="white-space:pre"> </span>} catch (Exception e) { <span style="white-space:pre"> </span>session.getTransaction().rollback(); <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>} finally { <span style="white-space:pre"> </span>HibernateUtil.closeSession(session); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}
发出sql:Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?) Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?) Hibernate: insert into t_role (role_name, role_id) values (?, ?) Hibernate: insert into t_user_role (role_id, user_id) values (?, ?) Hibernate: insert into t_user_role (role_id, user_id) values (?, ?)load
@Test public void loadTest(){ Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); Role role=(Role)session.load(Role.class,2); System.out.println("角色名称"+role.getRoleName()); System.out.println("users:"+role.getUsers()); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } }
发出sql:Hibernate: select role0_.role_id as role1_3_0_, role0_.role_name as role2_3_0_ from t_role role0_ where role0_.role_id=? 角色名称开发组 Hibernate: select users0_.role_id as role1_3_1_, users0_.user_id as user2_1_, user1_.user_id as user1_2_0_, user1_.user_name as user2_2_0_, user1_.password as password2_0_, user1_.student_id as student4_2_0_ from t_user_role users0_ inner join t_user user1_ on users0_.user_id=user1_.user_id where users0_.role_id=? users:[com.demo.model.User@b4be3d, com.demo.model.User@35bb0f]
hibernate之 10.many2many单向
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。