Hibernate集合映射学习

来源:互联网 发布:为什么要使用数组 编辑:程序博客网 时间:2024/06/05 17:36

测试类:

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(){@Overridepublic void doSession(Session session) {// TODO Auto-generated method stubCollectionMapping 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中测试了

复制搜索
原创粉丝点击