一、简介
前面讲的一对多或多对一等等,其中集合中必须放的是实体类,如果放的是字符串则不叫一对多或多对一映射。这里我们在集合中存储的都不是实体类,存储的是字符串集合,这就叫集合映射。这类映射在实际中用的比较少,但是需要了解其映射方式。主要注意各类集合的配置方式。
实例(工程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);
}
}
}
说明:可以看到在存储时和一对多或多对一的区别主要是不需要显式的存储集合,当在存储CollectionMapping类时会自动发送相关的sql语句将集合中的信息存入到相关的表中。查询的时候我们可以一次性将所以集合信息都查询出来。