详解hibernate双向多对多关联映射XML与注解版

论坛 期权论坛 脚本     
niminba   2021-5-23 02:47   1639   0

双向多对多关联映射原理:

假设,一个员工可能有多个角色,一个角色可能有多个员工,从员工或角色的角度看,这就是多对多的关系,不管从哪一个角度看,都是多对多的联系。多对多关联映射关系一般采用中间表的形式来实现,即新增一种包含关联双方主键的表。实现多对多关联关系,在数据库底层通过添加中间表指定关联关系,而在hibernate框架在双方的实体中添加一个保存对方的集合,在双方的映射文件中使用<set>元素和<many-to-many>元素进行关联关系的配置。

如下图所示:

(1)XML版

Role类:

package Hibernate_demo1.Demo15.Entity; 
 
import java.util.Set; 
 
public class Role { 
   
  private String id; 
  private String rame; 
  private Set<User> users; 
   
   
  public String getId() { 
    return id; 
  } 
  public void setId(String id) { 
    this.id = id; 
  } 
  public String getRame() { 
    return rame; 
  } 
  public void setRame(String rame) { 
    this.rame = rame; 
  } 
  public Set<User> getUsers() { 
    return users; 
  } 
  public void setUsers(Set<User> users) { 
    this.users = users; 
  } 
} 

User类:

package Hibernate_demo1.Demo15.Entity; 
 
import java.util.Set; 
 
public class User { 
 
  private String id; 
  private String uname; 
  private Set<Role> roles; 
 
   
 
  public String getId() { 
    return id; 
  } 
  public void setId(String id) { 
    this.id = id; 
  } 
  public String getUname() { 
    return uname; 
  } 
  public void setUname(String uname) { 
    this.uname = uname; 
  } 
  public Set<Role> getRoles() { 
    return roles; 
  } 
  public void setRoles(Set<Role> roles) { 
    this.roles = roles; 
  } 
   
   
} 

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="Hibernate_demo1.Demo15.Entity.Role" table="role">  
    <id name="id" type="java.lang.String">  
      <column name="id"/> 
      <generator class="uuid">  
      </generator>  
    </id>  
    <property name="rame" column="rname"/> 
     
    <set name="users" table="user_role">  
      <key column="roleid"></key>  
      <many-to-many class="Hibernate_demo1.Demo15.Entity.User" column="userid"></many-to-many>  
    </set>  
  </class>  
</hibernate-mapping>  

User.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="Hibernate_demo1.Demo15.Entity.User" table="user">  
    <id name="id">  
      <generator class="uuid">  
      </generator>  
    </id>  
     
    <property name="uname" column="uname"/>  
      
     <set name="roles" table="user_role"> 
      <key column="userid"/> 
      <many-to-many class="Hibernate_demo1.Demo15.Entity.Role" column="roleid" />   
    </set> 
  </class>  
</hibernate-mapping>  

测试类:

package Hibernate_demo1.Demo15; 
 
import java.util.HashSet; 
import java.util.Set; 
 
import org.hibernate.Session; 
 
import Hibernate_demo1.Demo15.Entity.Role; 
import Hibernate_demo1.Demo15.Entity.User; 
import Hibernate_demo1.Demo15.Util.HibernateUtils; 
 
 
 
public class App  
{ 
  public static void main( String[] args ) 
  { 
     
    Session session = null; 
    try{  
        
      session = HibernateUtils.getSession(); 
      session.beginTransaction();  
       
 
      Role r1 = new Role(); 
      r1.setRame("数据录入人员"); 
      session.save(r1); 
       
      Role r2 = new Role(); 
      r2.setRame("商务主管"); 
      session.save(r2); 
       
      Role r3 = new Role(); 
      r3.setRame("商务经理"); 
      session.save(r3); 
       
      Role r4 = new Role(); 
      r4.setRame("项目会计"); 
      session.save(r4); 
       
      User u1 = new User(); 
      u1.setUname("张三"); 
      Set<Role> u1Roles = new HashSet<Role>(); 
      u1Roles.add(r1); 
      u1Roles.add(r2); 
      u1.setRoles(u1Roles); 
      session.save(u1); 
       
      User u2 = new User(); 
      u2.setUname("李四"); 
      Set<Role> u2Roles = new HashSet<Role>(); 
      u2Roles.add(r1); 
      u2Roles.add(r2); 
      u2Roles.add(r3); 
      u2.setRoles(u2Roles); 
      session.save(u2); 
       
      User u3 = new User(); 
      u3.setUname("王五"); 
      Set<Role> u3B[\]K][BB[\X\]K[B[\X\]K[XNB[\X\]K[[B[\X\]KY][BB[\X\]W[[[[[]KNB[\X\]W[[[[[]K\BBBXX\\BBXX]XXZ[[H\HBBB[XH[XHH]Y][
KYJ
KZ[[XJ
NB[[H[XK[[
NB[[H[Y[[
NBBBb&)9.*-B\\[]\
NB\[[YJl#NBB\\[]\
NB\[[YJl#NBBb&*9-"B	[]\	NBY
\NBY
\NBBBb&)9.*)lBH[]J
NB[YJnNB\NBBH[]J
NB[YJ9+!NB\NBBBb&lf"B	I[]\	INBY
NBY
NBBo9-:)lf"B\\NB\\NBB/f9-lB[J\NB[J\NB[JNB[JNBBB[Z]

NB[J
NBHBBHBBOB]Bi9i."B]\HHBH\H\Z[BX\]NB[B[B\B
[[YKY
HB[Y\B
		HBX\]NB[B[B\B
[[YKY
HB[Y\B
		HBX\]NB[B[BHB
[YKY
HB[Y\B
		HBX\]NB[B[BHB
[YKY
HB[Y\B
		HBX\]NB[B[B\HB
\YZY
HB[Y\B
		HBX\]NB[B[B\HB
\YZY
HB[Y\B
		HBX\]NB[B[B\HB
\YZY
HB[Y\B
		HBX\]NB[B[B\HB
\YZY
HB[Y\B
		HBBOB]B."l,y+j:`yk{n#9&)9ki.h9"y`9n+b{.g&i&i&+/c.
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP