紧承以前的学习,继续了解hibernate对集合的操作学习之旅!
在上一篇中,就已经提前说过,这一篇的内容以及下一篇的内容会有很多相似之处,这里就不再多言了。像往常一样,新建一个StudentCollection.java实体类:
package com.joe.entity;
import java.util.Collection;
public class StudentCollection {
private int id;
private String name;
private int age;
private Collection<String> hobby;
/**
* 无参构造
*/
public StudentCollection(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Collection<String> getHobby() {
return hobby;
}
public void setHobby(Collection<String> hobby) {
this.hobby = hobby;
}
}
在StudentCollection.hbm.xml实体关系映射文件配置Colllection的时候,有两种不同的配置(<bag>或<idbag>元素),照例先看看有哪些常用的属性和子元素吧,<bag>或<idbag>元素用来映射java.util.Collection类型的属性,常用的属性和子元素有:
1.name属性
2.table属性
3.<collection-id>子元素,对应<idbag>
4.<key>子元素
5.<element>子元素
先看看一般比较常见的配置,也就是使用<idbag>进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "" >
<hibernate-mapping>
<!-- 一个class标签对应一个实体类,name属性指定实体类名称,table属性指定关联的数据库表 -->
<class table="stu_collect_tab">
<!-- 主键 -->
<id column="stu_id">
<!-- 提供ID自增的策略 native会根据数据库自行判断 -->
<generator></generator>
</id>
<!-- 其他属性,name对应实体类的属性,column对应关系型数据库表的列 -->
<property column="stu_name"></property>
<property column="stu_age"></property>
<!-- 使用idbag标签配置映射 -->
<idbag table="hobby_collect_tab">
<collection-id type="string" column="hobby_id">
<generator></generator>
</collection-id>
<key column="student_id"></key>
<element type="string" column="hobby"></element>
</idbag>
</class>
</hibernate-mapping>
另一种使用<bag>标签来配置映射关系的代码:
<!-- 使用bag标签配置映射 -->
<bag table="hobby_collect_tab">
<key column="student_id"></key>
<element type="string" column="hobby"></element>
</bag>
关于两者的不同,我会在测试类中分析,先看看两种配置生成的表结构吧:
使用<idbag>配置生成的表结构:
而使用<bag>标签进行配置的时候,生成的表结构又是什么样的呢?
可以发现,对比使用两种不同的方式创建的表结构,不难发现在使用<idbag>方式创建表结构的时候会增加一个额外的字段来维护表中的数据,单从这一点上就可以发现两者的不同。
当我们删除其中的某一个对象的一个hobby时,看看hibernate执行的操作:
@Test
public void findAll(){
Transaction tx=null;
Session session=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
StudentCollection stu=(StudentCollection)session.get(StudentCollection.class, 1);
System.out.println(stu.getName()+stu.getAge()+stu.getId());
Collection<String> c=stu.getHobby();
c.remove("swimming");
tx.commit();
}catch(HibernateException he){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
测试该方法,使用<bag>标签配置的情况是:
使用<idbag>进行配置的时候,结果是:
查询基本信息的时候,没有什么差别,关键是后面删除操作,对比两种方式,可以发现,使用<bag>方式配置的时候,hibernate采用的方式 是先获取对用student_id的所有hobby,然后全部删除,然后再session提交事务的时候,根据session管理的对象中数据差异将collection集合中的数据重新插入数据库;使用<idbag>方式配置时只会根据hobby_id的值去删除对应的数据,通过对比可以发现,使用<idbag>方式进行配置时会更合理,也会提高业务处理的效率,所以一般会使用<idbag>方式来配置。