欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 16:46:16
双向多对多关联映射原理: 假设,一个员工可能有多个角色,一个角色可能有多个员工,从员工或角色的角度看,这就是多对多的关系,不管从哪一个角度看,都是多对多的联系。多对多关联...

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

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

如下图所示:

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

(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> u3roles = new hashset<role>(); 
      u3roles.add(r3); 
      u3roles.add(r4); 
      u3.setroles(u3roles); 
      session.save(u3); 
       
       
      session.gettransaction().commit();  
    }catch(exception e){  
      e.printstacktrace();  
      session.gettransaction().rollback();  
    }finally{  
      hibernateutils.closesession(session);  
    } 
  } 
} 

执行结果:

hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user 
    (uname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user 
    (uname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user 
    (uname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 

数据库内容为:

user表:

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

role表:

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

user_role表:

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

(2)注解版

role类:

package hibernate_demo1.demo16.entity; 
 
import java.util.hashset; 
import java.util.set; 
 
import javax.persistence.cascadetype; 
import javax.persistence.column; 
import javax.persistence.entity; 
import javax.persistence.generatedvalue; 
import javax.persistence.id; 
import javax.persistence.manytomany; 
import javax.persistence.table; 
 
import org.hibernate.annotations.genericgenerator; 
 
@entity 
@table(name="role") 
public class role { 
   
  @id 
    @genericgenerator(name="uuidgenerator", strategy="uuid") 
    @generatedvalue(generator="uuidgenerator") 
  private string id; 
   
  @column(name="rname") 
  private string rame; 
   
  @manytomany(cascade=cascadetype.refresh,mappedby="roles") 
  private set<user> users=new hashset<user>(); 
   
   
   
   
  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.demo16.entity; 
 
import java.util.hashset; 
import java.util.set; 
 
import javax.persistence.cascadetype; 
import javax.persistence.column; 
import javax.persistence.entity; 
import javax.persistence.generatedvalue; 
import javax.persistence.id; 
import javax.persistence.jointable; 
import javax.persistence.manytomany; 
import javax.persistence.table; 
import javax.persistence.joincolumn; 
 
import org.hibernate.annotations.genericgenerator; 
 
@entity 
@table(name="user") 
public class user { 
 
  @id 
    @genericgenerator(name="uuidgenerator", strategy="uuid") 
    @generatedvalue(generator="uuidgenerator") 
  private string id; 
   
  @column(name="uname") 
  private string uname; 
   
  /* 
   * @manytomany表示多对多关联,对于这种关联极少数情况会使用级联删除,我这里设置的是级联刷新; 
   * 因为有中间表的存在这里使用@jointable来设置关联表后面的name配置的是关联表的名称 
   * inversejoincolumn配置的是关系被维护一方主键对应的中间表字段 
   * joincolumn配置的是关系维护方主键对应的中间表字段。 
   */ 
  @manytomany(cascade=cascadetype.refresh) 
  @jointable(name="user_role",inversejoincolumns=@joincolumn(name="roleid"),joincolumns=@joincolumn(name="userid")) 
  private set<role> roles=new hashset<role>(); 
 
   
 
  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; 
  } 
   
   
} 

测试类:

package hibernate_demo1.demo16; 
 
import java.util.hashset; 
import java.util.set; 
 
import org.hibernate.session; 
import org.hibernate.sessionfactory; 
import org.hibernate.transaction; 
import org.hibernate.cfg.configuration; 
 
import hibernate_demo1.demo16.entity.role; 
import hibernate_demo1.demo16.entity.user; 
 
 
public class test { 
 
  public static void main(string[] args) { 
     
 
    sessionfactory sessionfactory = new configuration().configure().buildsessionfactory();  
    session session = sessionfactory.getcurrentsession();  
    transaction tx = session.begintransaction();  
     
   
    //创建两个用户 
    user us1=new user(); 
    us1.setuname("小明"); 
     
    user us2=new user(); 
    us2.setuname("小黑"); 
     
    //创建用户集合 
    set<user> su=new hashset<user>(); 
    su.add(us1); 
    su.add(us2); 
     
     
    //创建两个角色 
    role ro1=new role(); 
    ro1.setrame("程序员"); 
    ro1.setusers(su); 
     
    role ro2=new role(); 
    ro2.setrame("技术经理"); 
    ro2.setusers(su); 
     
     
    //创建角色集合 
    set<role> sr=new hashset<role>(); 
    sr.add(ro1); 
    sr.add(ro2); 
     
    //往用户添加角色集合 
    us1.setroles(sr); 
    us2.setroles(sr); 
     
    //保存用户和角色 
    session.save(us1); 
    session.save(us2); 
    session.save(ro1); 
    session.save(ro2); 
     
     
    tx.commit(); 
    session.close(); 
  } 
 
} 

执行结果如下:

hibernate:  
  insert  
  into 
    user 
    (uname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user 
    (uname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    role 
    (rname, id)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 
hibernate:  
  insert  
  into 
    user_role 
    (userid, roleid)  
  values 
    (?, ?) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。