您的位置:首页 > 其它

Hibernate集合映射学习

2012-03-08 18:43 363 查看
测试类:

public class CollectionMapping {
private int id;
private String cname;
private String[] arrayValue;
private List listValue;
private Set SetValue;
private Map mapValue;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Set getSetValue() {
return SetValue;
}
public void setSetValue(Set setValue) {
SetValue = setValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
HibernateTemplate.execute(new SessionOperate(){
@Override
public void doSession(Session session) {
// TODO Auto-generated method stub
CollectionMapping collectionMapping = new CollectionMapping();
collectionMapping.setCname("c");
String[] array = {"liu","li"};
collectionMapping.setArrayValue(array);
List list = new ArrayList();
list.add("l1");
list.add("l2");
collectionMapping.setListValue(list);
Set set = new HashSet();
set.add("s1");
set.add("s2");
collectionMapping.setSetValue(set);
Map map = new HashMap();
map.put("k1", "v1");
map.put("k2", "v2");
collectionMapping.setMapValue(map);
session.save(collectionMapping);
}
});
}
}


配置文件:

<hibernate-mapping package="com.pk.po">
<class name="CollectionMapping" table="tb_col_mapping">
<id name="id">
<generator class="native"></generator>
</id>
<property name="cname"></property>
<array name="arrayValue" table="t_array">
<key column="arrayId"></key>
<list-index column="arrayIndex"></list-index>
<element column="arrayValue" type="string"></element>
</array>

<list name="listValue" table="t_list">
<key column="listId"></key>
<list-index column="listIndex"></list-index>
<element column="listValue" type="string"></element>
</list>

<set name="SetValue" table="t_set">
<key column="setId"></key>
<element column="setValue" type="string" ></element>
</set>

<map name="mapValue" table="t_map">
<key column="mapId"></key>
<map-key column="mapKey" type="string"></map-key>
<element column="mapValue" type="string"></element>
</map>
</class>

</hibernate-mapping>


由于简化,便直接在po中测试了

复制搜索

复制搜索
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: