一、簡介
前面講的一對多或多對一等等样悟,其中集合中必須放的是實體類拂募,如果放的是字符串則不叫一對多或多對一映射。這里我們在集合中存儲的都不是實體類窟她,存儲的是字符串集合陈症,這就叫集合映射。這類映射在實際中用的比較少震糖,但是需要了解其映射方式录肯。主要注意各類集合的配置方式。
實例(工程hibernate_collection_mapping
)
相關映射:
CollectionMapping.java
private int id;
private String name;
private Set setValue;
private List listValue;
private String[] arrayValue;
private Map mapValue;
CollectionMapping.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="cn.itcast.hibernate.CollectionMapping" table="_CollectionMapping">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="setValue" table="_set_value">
<key column="set_id"/>
<element type="string" column="set_value"/>
</set>
<list name="listValue" table="_list_value">
<key column="list_id"/>
<list-index column="list_index"/>
<element type="string" column="list_value"/>
</list>
<array name="arrayValue" table="_array_value">
<key column="array_id"/>
<list-index column="array_index"/>
<element type="string" column="array_value"/>
</array>
<map name="mapValue" table="_map_value">
<key column="map_id"/>
<map-key type="string" column="map_key"/>
<element type="string" column="map_value"/>
</map>
</class>
</hibernate-mapping>
說明:這里在數據庫中會生成五張表吊说,其中各個集合分別生成一張表论咏,而CollectionMapping類生成一張表。其主鍵分別作為各個集合表的外鍵颁井。
測試:
CollectionMappintTest.java
package cn.itcast.hibernate;
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 CollectionMappintTest extends TestCase {
public void testSave1() {
Session session = null;
CollectionMapping c = new CollectionMapping();
c.setName("xxx");
Set setValue = new HashSet();
setValue.add("a");
setValue.add("b");
c.setSetValue(setValue);
List listValue = new ArrayList();
listValue.add("c");
listValue.add("d");
c.setListValue(listValue);
String[] arrayValue = new String[]{"e", "f"};
c.setArrayValue(arrayValue);
Map mapValue = new HashMap();
mapValue.put("k1", "v1");
mapValue.put("k2", "v2");
c.setMapValue(mapValue);
try {
session = HibernateUtils.getSession();
session.beginTransaction();
session.save(c);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
CollectionMapping c = (CollectionMapping)session.load(CollectionMapping.class, 1);
System.out.println("name=" + c.getName());
System.out.println("setvalue=" + c.getSetValue());
System.out.println("mapvalue=" + c.getMapValue());
System.out.println("listvalue=" + c.getListValue());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說明:可以看到在存儲時和一對多或多對一的區(qū)別主要是不需要顯式的存儲集合厅贪,當在存儲CollectionMapping類時會自動發(fā)送相關的sql語句將集合中的信息存入到相關的表中。查詢的時候我們可以一次性將所以集合信息都查詢出來雅宾。