Set
HashSet 无序,不重复
e_user e_user_addressSet
id userId
name address
要说明的信息:
a:集合表的名称(集合表)
b:集合表中的外键(集合外键)
c:集合表中的元素列(集合元素)
List
有序,可重复
e_user e_user_addressList
id userId
name address
idx
要说明的信息:
a:集合表的名称(集合表)
b:集合表中的外键(集合外键)
c:集合表中的元素列(集合元素)
e:集合元素的索引列
Map
HashMap 无序,不重复(以key为准)
e_user e_user_addressMap
id userId
name address
key_
要说明的信息:
a:集合表的名称(集合表)
b:集合表中的外键(集合外键)
c:集合表中的元素列(集合元素)
e:集合元素的键
-------------------------------------------------------------------
数组
有序,可重复
e_user e_user_addressArray
id userId
name address
idx
要说明的信息:
a:集合表的名称(集合表)
b:集合表中的外键(集合外键)
c:集合表中的元素列(集合元素)
e:集合元素的索引列
Bag
无序,可重复
List
e_user e_user_addressBag
id userId
name address
要说明的信息:
a:集合表的名称(集合表)
b:集合表中的外键(集合外键)
c:集合表中的元素列(集合元素)
使用集合属性时,一定要使用接口,而不能声明为具体的实现类。
因为经过Session操作后,集合变成了Hibernate自已的集合实现类。
映射文件
<?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 package="cn.itcast.e_hbm_collection">
<!-- 类名 -->
<class name="User" table="e_user">
<id name="id" type="integer" column="id_">
<generator class="native" />
</id>
<property name="name" type="string" column="name_" />
<!-- addressSet属性,Set集合
table属性:集合表的名称
key子元素:集合外键的列名
element子元素:存放集合元素列的信息
-->
<set name="addressSet" table="e_user_addressSet">
<key column="userId"></key>
<element type="string" column="address_"></element>
</set>
<!-- addressList属性,List集合 list-index:用于存放索引的列 -->
<list name="addressList" table="e_user_addressList">
<key column="userId" />
<list-index column="idx_"></list-index>
<element type="string" column="address_" />
</list>
<!-- addressArray属性,数组。与List映射基本一致 -->
<array name="addressArray" table="e_user_addressArray">
<key column="userId"></key>
<index type="string" column="idx_"></index>
<element type="string" column="address_"></element>
</array>
<!-- addressMap属性,Map集合 -->
<map name="addressMap" table="e_user_addressMap">
<key column="userId"></key>
<map-key type="string" column="key_"></map-key>
<element type="string" column="address_"></element>
</map>
<!-- addressBag属性,Bag集合:无序,可重复。与Set集合的映射基本一致 -->
<bag name="addressBag" table="e_user_addressBag">
<key column="userId"></key>
<element type="string" column="address_"></element>
</bag>
</class>
</hibernate-mapping>
实体类
package cn.itcast.e_hbm_collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class User {
private Integer id;
private String name;
private Set<String> addressSet = new HashSet<String>();// Set集合
private List<String> addressList = new ArrayList<String>();// List集合
private String[] addressArray;// 数组
private Map<String, String> addressMap = new HashMap<String, String>();// Map集合
private List<String> addressBag = new ArrayList<String>();// Bag集合
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<String> getAddressSet() {
return addressSet;
}
public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}
public List<String> getAddressList() {
return addressList;
}
public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}
public String[] getAddressArray() {
return addressArray;
}
public void setAddressArray(String[] addressArray) {
this.addressArray = addressArray;
}
public Map<String, String> getAddressMap() {
return addressMap;
}
public void setAddressMap(Map<String, String> addressMap) {
this.addressMap = addressMap;
}
public List<String> getAddressBag() {
return addressBag;
}
public void setAddressBag(List<String> addressBag) {
this.addressBag = addressBag;
}
}
保存获取操作类
package cn.itcast.e_hbm_collection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sessionFactory = new Configuration()//
.configure()//
.addClass(User.class)//
.buildSessionFactory();
@Test
public void testSave() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------
// 操作
Set<String> set = new HashSet<String>();
set.add("骆家庄东苑");
set.add("文一新西路");
// 构建对象
User user = new User();
user.setName("李四");
// >> Set集合
user.getAddressSet().add("新青年厂场");
user.getAddressSet().add("新塘路");
// >> List集合
user.getAddressList().add("新青年厂场");
user.getAddressList().add("新塘路");
user.getAddressList().add("新塘路");
// >> 数组
String[] string = { "新青年厂场", "新塘路", "新塘路" };
user.setAddressArray(string);
// >> Map集合
user.getAddressMap().put("公司", "新青年厂场");
user.getAddressMap().put("家庭", "骆家庄东苑");
// >> Bag集合
user.getAddressBag().add("新青年厂场");
user.getAddressBag().add("新塘路");
user.getAddressBag().add("新塘路");
// 保存
session.save(user);
// ------------------------------------
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
session.close();
}
}
@Test
public void testGet() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------
// 获取数据
User user = (User) session.get(User.class, 1);
// 显示信息
System.out.println(user.getId());
System.out.println(user.getName());
// >> Set集合
System.out.println(user.getAddressSet());
// >> List集合
System.out.println(user.getAddressList().get(0));
// >> 数组
System.out.println(Arrays.toString(user.getAddressArray()));
// >> Map集合
System.out.println(user.getAddressMap());
// >> Bag集合
System.out.println(user.getAddressBag());
// ------------------------------------
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
session.close();
}
}
}