CollectionMapping 类
public class CollectionMapping {
// 私有对象private Integer id;
private String name;
private String[] strValues;
private List<String> listValues = new ArrayList<String>();
private Set<String> setValues = new HashSet<String>();
private Map<String,String> mapValues = new HashMap<String, String>();
// 封装对象
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 String[] getStrValues() {
return strValues;
}
public void setStrValues(String[] strValues) {
this.strValues = strValues;
}
public List<String> getListValues() {
return listValues;
}
public void setListValues(List<String> listValues) {
this.listValues = listValues;
}
public Set<String> getSetValues() {
return setValues;
}
public void setSetValues(Set<String> setValues) {
this.setValues = setValues;
}
public Map<String, String> getMapValues() {
return mapValues;
}
public void setMapValues(Map<String, String> mapValues) {
this.mapValues = mapValues;
}
}
CollectionMapping.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" >
<hibernate-mapping package="com.bjsxt.hibernate">
<class name="CollectionMapping" table="t_collection">
<id name="id">
<column name="id"></column>
<generator class="native"></generator>
</id>
<property name="name">
<column name="name"></column>
</property>
<!-- 配置数组关联映射 -->
<array name="strValues" table="t_array">
<key column="aid"></key> <!-- 对应主表的id,也就是数据表t_collection的主键id值 -->
<list-index column="indexId"></list-index> <!-- 数组的索引值 -->
<element type="java.lang.String" column="realValue"></element> <!-- 在该数组表中存放的实际的值 -->
</array>
<!-- 配置List关联映射 -->
<list name="listValues" table="t_list">
<key column="lid"></key>
<list-index column="indexId"></list-index>
<element type="java.lang.String" column="realValue"></element>
</list>
<!-- 配置Map关联映射 -->
<map name="mapValues" table="t_map">
<key column="mid"></key>
<map-key type="java.lang.String" column="keyId"></map-key>
<element type="java.lang.String" column="realValue"></element>
</map>
<!-- 配置Set关联映射 -->
<set name="setValues" table="t_set">
<key column="sid"></key>
<element type="java.lang.String" column="realValue"></element>
</set>
</class>
</hibernate-mapping>
测试代码
public static void main(String[] args) {
SessionFactory sessionFactory = null;
new SchemaExport(HibernateSessionFactory.getConfiguration()).create(false, true);
sessionFactory = HibernateSessionFactory.getSessionFactory();
// 创建数组对象
String[] strArray = new String[3];
strArray[0]="cheng";
strArray[1]="zeng";
strArray[2]="zhang";
// 创建List对象
List<String> dataList = new ArrayList<String>();
dataList.add("s1");
dataList.add("s2");
dataList.add("y2");
// 创建Set对象
Set<String> dataSet = new HashSet<String>();
dataSet.add("Microsoft");
dataSet.add("Google");
dataSet.add("IBM");
// 创建Map对象
Map<String,String> dataMap = new HashMap<String, String>();
dataMap.put("America", "美国");
dataMap.put("China", "中国");
dataMap.put("Russia", "俄国");
// 创建CollectionMapping对象
CollectionMapping cm = new CollectionMapping();
// 设置CollectionMapping对象的值
cm.setName("我的大学");
cm.setListValues(dataList);
cm.setMapValues(dataMap);
cm.setSetValues(dataSet);
cm.setStrValues(strArray);
// 创建Session对象
Session session = sessionFactory.getCurrentSession();
// 执行添加操作
session.save(cm);
// 提交事务
session.beginTransaction().commit();
// 关闭事务
}
数据库