Hibernate学习22 -- 关联映射12 -- 集合映射
2009-12-04 12:45
537 查看
在对象中有普通属性,也有可能有集合属性如set、list、array、map,那我们在hibernate中该如何映射呢?我们存储应当如下面显示:
代码实现:
1) 建立一个对象,里面包含普通属性也有集合属性,如list、set、array、map,例如:CollectionMapping.java
2) 建立映射文件,例如:CollectionMapping.hbm.xml
3) 测试代码:
代码实现:
1) 建立一个对象,里面包含普通属性也有集合属性,如list、set、array、map,例如:CollectionMapping.java
public class CollectionMapping { private int id; private String name; private Set setValue; private List listValue; private String[] arrayValue; private Map mapValue; |
<hibernate-mapping> <class name="hibernate.CollectionMapping" table="t_CollectionMapping"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="setValue" table="t_set_value"> <key column="set_id"/> <element type="string" column="set_value"/> </set> <list name="listValue" table="t_list_value"> <key column="list_id"/> <list-index column="list_index"/> <element type="string" column="list_value"/> </list> <array name="arrayValue" table="t_array_value"> <key column="array_id"/> <list-index column="array_index"/> <element type="string" column="array_value"/> </array> <map name="mapValue" table="t_map_value"> <key column="map_id"/> <map-key type="string" column="map_key"/> <element type="string" column="map_value"/> </map> </class> </hibernate-mapping> |
public class CollectionMappintTest extends TestCase { public void testSave1() { Session session = null; CollectionMapping c = new CollectionMapping(); c.setName("xxx"); Set setValue = new HashSet(); setValue.add("a"); setValue.add("b"); c.setSetValue(setValue); List listValue = new ArrayList(); listValue.add("c"); listValue.add("d"); c.setListValue(listValue); String[] arrayValue = new String[]{"e", "f"}; c.setArrayValue(arrayValue); Map mapValue = new HashMap(); mapValue.put("k1", "v1"); mapValue.put("k2", "v2"); c.setMapValue(mapValue); try { session = HibernateUtils.getSession(); session.beginTransaction(); session.save(c); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); CollectionMapping c = (CollectionMapping)session.load (CollectionMapping.class, 1); System.out.println("name=" + c.getName()); System.out.println("setvalue=" + c.getSetValue()); System.out.println("mapvalue=" + c.getMapValue()); System.out.println("listvalue=" + c.getListValue()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } } |
相关文章推荐
- hibernate学习12之Collection(集合)映射
- Hibernate学习12 -- 关联映射2 -- 一对一关联1 -- 一对一主键关联
- Hibernate学习_009_一对一关联映射
- Hibernate学习-7-关联映射(一对多,多对一)
- 【HIbernate框架学习】:Hibernate对象关系映射之一对一关联映射(二)
- hibernate中关联映射的学习总结
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
- hibernate学习笔记-2入门初体验关联映射
- Hibernate学习笔记-----关系映射(2)单向1-1关联
- Hibernate学习18 -- 关联映射8 -- 继承映射2 -- 父类存储在一张表,子类分别存储一张表
- Hibernate 学习笔记(3):关联映射之多对一:@ManyToOne
- Hibernate学习随笔-----关系映射(3)单向1-N关联
- 【Hibernate学习笔记】映射一对一关联与多对多关联
- hibernate中集合映射关联映射小记
- Hibernate 学习笔记07 --集合映射_&_继承映射
- SSH与SSM学习之hibernate23——关联级别加载策略之集合关联加载策略
- hibernate 6 映射集合和实体关联 | hibernate 实战(第二版) 第6章映射集合和实体关联 | 笔记
- Hibernate学习之---Hibernate的关联映射
- Hibernate集合映射、关联映射
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)