Hibernate集合映射之IDBag

系统 1551 0

假设Team和Student是1对多的关系,而student中只有team_id和name两个属性,我们可以不建立Student实体类,采用element的方式,由于bag可以允许重复,所以,在我们根据team删除其下某一个student的时候,hibernate不知道具体要删除哪一条数据,所以,只有讲team下所有student全部删除,然后再重新插入不应该删除的数据,这样会对效率有很大影响,针对这种情况,可以采用idbag标签,在student中加一个字段cid来唯一标识每一个student

数据库结构:

 

create   table  teamIDBag (id  varchar ( 32 ),teamname  varchar ( 32 ));
create   table  studentIDBag(team_id  varchar ( 32 ),name  varchar ( 32 ),cid  varchar ( 32 ));

Pojo:

 

package  Collection.IDBag;

import  java.util.ArrayList;
import  java.util.List;
import  java.util.Map;

public   class  Team  ... {
   
private  String id;
   
private  String teamname;
   
private  List students = new  ArrayList();
public  String getId()  ... {
    
return  id;
}

public   void  setId(String id)  ... {
    
this .id  =  id;
}

public  String getTeamname()  ... {
    
return  teamname;
}

public   void  setTeamname(String teamname)  ... {
    
this .teamname  =  teamname;
}

public  List getStudents()  ... {
    
return  students;
}

public   void  setStudents(List students)  ... {
    
this .students  =  students;
}


}

 Team.hbm.xml

 

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
< class  name ="Collection.IDBag.Team"  table ="teamIDBag"   >
    
< id  name ="id"  unsaved-value ="null" >
      
< generator  class ="uuid.hex" ></ generator >
    
</ id >
    
< property  name ="teamname"  type ="string"  column ="teamname" ></ property >
   
    
< idbag  name ="students"  table ="studentIDBag"  cascade ="all,delete-orphan"   >
      
< collection-id  type ="string"  column ="cid" >
        
< generator  class ="uuid.hex" ></ generator >
      
</ collection-id >
      
< key  column ="team_id" ></ key >
      
< element  type ="string"  column ="name" ></ element >
    
</ idbag >
</ class >

</ hibernate-mapping >

 

Hibernate.cfg.xml

 

<? xml version='1.0' encoding='UTF-8' ?>
<! DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<!--  Generated by MyEclipse Hibernate Tools.                    -->
< hibernate-configuration >

< session-factory >
    
< property  name ="connection.username" > root </ property >
    
< property  name ="connection.url" >
        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp; useUnicode=true
    
</ property >
    
< property  name ="dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="myeclipse.connection.profile" > mysql </ property >
    
< property  name ="connection.password" > 1234 </ property >
    
< property  name ="connection.driver_class" >
        com.mysql.jdbc.Driver
    
</ property >
    
< property  name ="hibernate.dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="hibernate.show_sql" > true </ property >
    
< property  name ="current_session_context_class" > thread </ property >
    
< property  name ="jdbc.batch_size" > 15 </ property >
    
< mapping  resource ="Collection/IDBag/Team.hbm.xml"   />




</ session-factory >

</ hibernate-configuration >

 

测试代码:

 

package  Collection.IDBag;


import  java.io.File;
import  java.util.Map;

import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;

public   class  Test  ... {


    
public   static   void  main(String[] args)  ... {

        String filePath
= System.getProperty( " user.dir " ) + File.separator + " src/Collection/IDBag " + File.separator + " hibernate.cfg.xml " ;
        File file
= new  File(filePath);
        System.out.println(filePath);
        SessionFactory sessionFactory
= new  Configuration().configure(file).buildSessionFactory();
        Session session
= sessionFactory.openSession();
        Transaction t
= session.beginTransaction();
        
    
        
//         Team team1=new Team();
//         team1.setTeamname("team1");
//         
//         Team team2=new Team();
//         team2.setTeamname("team2");
//         
//        
//        
//
//          // 由于Bag映射可以重复,所以这里重复保存student
//         team1.getStudents().add("tom1");
//         team1.getStudents().add("tom1");
//         team2.getStudents().add("tom2");
//         
//         
//         
//         session.save(team1);
//         session.save(team2);
        

        Team t1
= (Team)session.createQuery( " from Team t where t.teamname='team1' " ).uniqueResult();
        t1.getStudents().remove(
0 );
        t.commit();
        
    }


}

 

运行结果:

Hibernate: select team0_.id as id0_, team0_.teamname as teamname0_ from teamIDBag team0_ where team0_.teamname='team1'
Hibernate: select students0_.team_id as team1_0_, students0_.name as name0_, students0_.cid as cid0_ from studentIDBag students0_ where students0_.team_id=?
Hibernate: delete from studentIDBag where cid=?

可以红色部分,删除student时是根据cid删的,如果不使用idbag,则会出类似delete from studentIDBag where team_id=?的操作,然后会接着跟一个insert操作,来恢复本不应该删除的数据

Hibernate集合映射之IDBag


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论