Hibernate常见的集合映射主要有Set,List,Array,Map,Bag等,它们的特点如下:
1) Set集合是Hibernate中基础的集合类型,元素数据一般使用外键同主表关联,Set集合非常适用于集合元素不能重复的情况。
2) Set集合是无序的,集合元素并不是按照一定的顺序排列的,而List集合是有序的,每个集合元素需要有一个表示集合序号的标识,这样可以使用该标识来获得该集合元素。
3) 数组Array使用的是List的方式来表示的。
4) Map集合的特点是使用键值对,即Key-Value结构来存放集合元素,这样就需要对应的集合元素数据库表中包含对于Key的列。
5) Set集合石无序、不能有重复元素的,Hibernate提供了一个Bag集合,用来处理重复元素的情况。值得注意的是,Bag并不是Java API,而是Hibernate提供的。Bag集合映射和List不同,List的集合元素是有序的,需要有一个集合序号来标识集合元素的位置,List元素可以重复;而Bag集合元素不需要元素序号标识,元素也是可以重复的。
首先建立数据库表,SQL语句如下:
CREATE TABLE ‘message’(
'id' int(11) NOT NULL,
'setValue' varchar(100) NOT NULL,
'listValue_id' int(11) NOT NULL,
'listValue' varchar(100) NOT NULL,
'arrayValue_id' int(11) NOT NULL,
'arrayValue' varchar(100) NOT NULL,
'mapValue_id' int(11) NOT NULL,
'mapValue' varchar(100) NOT NULL,
'bagValue' varchar(100) NOT NULL
)ENGINE = InnoDB DEFAULT CHARSET = gbk
CREATE TABLE 'master'(
'id' int(11) NOT NULL auto_increment,
'name' varchar(100) NOT NULL default '',
PRIMARY KEY('id')
)ENGINE = InnoDB DEFAULT CHARSET = gbk
建立角色的实体类,代码如下:
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({ "unchecked", "serial" })
public class Master implements Serializable {
private Integer id; //主键ID属性
private String name; //姓名name属性
private Set setValue;
private List listValue;
private String[] arrayValue;
private Map mapValue;
private List bagValue;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
public List getBagValue() {
return bagValue;
}
public void setBagValue(List bagValue) {
this.bagValue = bagValue;
}
}
类Message的Hibernate映射文件Message.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 Persistence Tools --> <hibernate-mapping> <class name="com.iprai.hibernate.collectionmapping" table="master" catalog="iprai"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" length="100" not-null="true" /> </property> <!--Set类型映射--> <set name="setValue" table="message"> //定义对应的表 <key column="id" /> <element type="java.lang.String"> <column name="setValue" /> </element> </set> <!--List类型映射--> <List name="listValue" table="message"> <key column="id" /> <index column="listValue_id" /> //配置主键 <element type="java.lang.String" column="listValue" /> </List> <!--Array类型映射--> <array name="arrayValue" table="message"> <key column="id"/> <list-index column="arrayValue_id"/> <element type="java.lang.String" column="arrayValue"/> </array> <!--Map类型映射--> <map name="mapValue" table="message"> <key column="id" /> <!--配置键值对--> <map-key column="mapValue_id" type="java.lang.String" /> <element type="java.lang.String" column="mapValue" /> </map> <!--Bag类型映射--> <bag name="bagValue" table="message"> <key column="id" /> //定义主键 <element type="string" column="bagValue" /> //定义元素类型 </bag> </class> </hibernate-mapping>
JUnit测试代码如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Session;
import junit.framework.TestCase;
public class MasterTest extends TestCase {
public void testSave() {
Master master = new Master();
master.setName("JavaEye");
Set setValue = new HashSet();
setValue.add("a");
setValue.add("b");
master.setSetValue(setValue);
List listValue = new ArrayList();
listValue.add("c");
listValue.add("d");
master.setListValue(listValue);
String[] arrayValue = new String[]{"e", "f"};
master.setArrayValue(arrayValue);
Map mapValue = new HashMap();
mapValue.put("key_1", "value_1");
mapValue.put("key_2", "value_2");
master.setMapValue(mapValue);
List bagValue = new ArrayList();
bagValue.add("h");
bagValue.add("i");
master.setBagValue(bagValue);
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
session.save(c);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}