假设Team和Student是1对多的关系,而student中只有team_id和name两个属性,我们可以不建立Student实体类,采用element的方式,由于bag可以允许重复,所以,在我们根据team删除其下某一个student的时候,hibernate不知道具体要删除哪一条数据,所以,只有讲team下所有student全部删除,然后再重新插入不应该删除的数据,这样会对效率有很大影响,针对这种情况,可以采用idbag标签,在student中加一个字段cid来唯一标识每一个student
数据库结构:
create table studentIDBag(team_id varchar ( 32 ),name varchar ( 32 ),cid varchar ( 32 ));
Pojo:
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
<! 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
<! 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 & 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 >
测试代码:
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操作,来恢复本不应该删除的数据