Hibernate集合映射学习

测试类:

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中测试了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值