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="com.itany.hibernate">

<!-- 不用默认表名,新定义一个表名 -->
<class name="CollectionMapping" table="t_collectionMapping">
<!-- 设置字段长度,用自定义列名 -->
<id name="id" length="32">
<generator class="native"/>
</id>
<property name="name" length="20"/>

<set name="setValues" table="t_setValues">
<key column="setId"></key>
<element type="string" column="value"></element>
</set>

<list name="listValues" table="t_listValues">
<key column="listId"></key>
<!-- 不能直接用index,他是关键字 -->
<list-index column="list_index"></list-index>
<element type="string" column="value"></element>
</list>

<array name="arrayValues" table="t_arrayValues">
<key column="arrayId"></key>
<!-- 不能直接用index,他是关键字 -->
<list-index column="array_index"></list-index>
<element type="string" column="array_value"></element>
</array>

<map name="mapValues" table="t_mapValues">
<key column="mapId"></key>
<map-key type="string" column="map_key"></map-key>
<element type="string" column="map_value"></element>
</map>
</class>

</hibernate-mapping>

 

在实体类中,将集合作为他的属性,

package com.itany.hibernate;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionMapping {

private int id;
private String name;
private Set setValues;
private List listValues;
private String[] arrayValues;
private Map mapValues;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getSetValues() {
return setValues;
}
public void setSetValues(Set setValues) {
this.setValues = setValues;
}
public List getListValues() {
return listValues;
}
public void setListValues(List listValues) {
this.listValues = listValues;
}
public String[] getArrayValues() {
return arrayValues;
}
public void setArrayValues(String[] ss) {
this.arrayValues = ss;
}
public Map getMapValues() {
return mapValues;
}
public void setMapValues(Map mapValues) {
this.mapValues = mapValues;
}
}

最后不要忘记在hibernate.cfg.xml引入配置

posted @ 2015-05-19 08:50  爱上咖啡的唐  阅读(139)  评论(0编辑  收藏  举报