1、Java的Hibernate框架中集合类数据结构的映射编写课案Java的Hibernate框架中集合类数据结构的映射编写教程Hibernate可以将Java中几个内置的集合结构映射为数据库使用的关系模型,下面我们就来看一下Java的Hibernate框架中集合类数据结构的映射编写教程:一、集合映射1.集合小介集合映射也是基本的映射,但在开发过程中不会经常用到,所以不需要深刻了解,只需要理解基本的使用方法即可,等在开发过程中遇到了这种问题时能够查询到解决方法就可以了。对应集合映射它其实是指将java中的集合映射到对应的表中,是一种集合对象的映射,在java中有四种类型的集合,分别是Set、Map
2、、List还有普通的数组,它们之间有很大的区别:(1)Set,不可以有重复的对象,对象是无序的;(2)List,可以与重复的对象,对象之间有顺序;(3)Map,它是键值成对出现的;(4)数组,可以重复,对象之间有顺序。它们之间的区别决定了在开发时使用哪种集合,通常在开发时会使用Set,它内部的对象是无需的,并可以使用迭代器获取内部对象。这几种集合想要映射到相应的关系模型的话就必须使用Hibernate提供的映射标签,、。2.映射小介继续讨论集合映射的关系模型,集合映射是指一个对象对应着另一个对象集合,在保存时Hibernate会把数据集合保存到相应的表中,并按照自己分配的id把数据保存到数据表
3、中,如果单独为集合分配了新表,那么会将id分配给集合表的id,那么对应的关系表如下图:3.类文件集合映射是如何通过代码实现的,接下来具体分析。这里把所有的集合封存到一个类中,这个类我们称之为CollectionMapping.java,那么它对应的内部代码如下:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263package com.hibernate; import java.util.List; import
4、 java.util.Map; import java.util.Set; SuppressWarnings(rawtypes) public class CollectionMapping /id private int id; public int getId() return id; public void setId(int id) this.id = id; /名字 private String name; public String getName() return name; public void setName(String name) this.name = name; /
5、Set集合 private Set setValues; public Set getSetValues() return setValues; public void setSetValues(Set setValues) this.setValues = setValues; /List集合 private List listValues; public List getListValues() return listValues; public void setListValues(List listValues) this.listValues = listValues; /数组集合
6、private String arrayValues; public String getArrayValues() return arrayValues; public void setArrayValues(String arrayValues) this.arrayValues = arrayValues; /Map集合 private Map mapValues; public Map getMapValues() return mapValues; public void setMapValues(Map mapValues) this.mapValues = mapValues;
7、该类中封装了几种常用的集合,想要转化为关系模型,就必须来看下文的映射。4.集合映射集合的映射其实相当的简单,只需要添加对应的集合标签,Hibernate分别提供了集合标签、,通过使用集中标签来将集合映射为对应的关系表,另外通过添加标签来实现表外键的关联,其它的属性通过使用来添加。CollectionMapping.hbm.xml1234567891011121314151617181920212223242526272829303132 !DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN 需要注意
8、的是list标签和array标签,这两种集合内的对象是有顺序的,所以在添加映射标签时需要使用list-index或者index标签来标明对象的顺序,而且在添加子标签时一定要按照顺序添加,也就是说先添加标签,后添加标签,最后添加标签,否则的话会出现如下错误:The content of element type list must match (meta*,subselect?,cache?,synchronize*,comment?,key,(index|list-index),(element|one-to-many|many-to-many|composite-element|many-t
9、o-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*).5.关系模型将配置好的对象模型转化为相应的关系模型,生成的SQL语句如下:123456789101112131415161718alter table t_array_value drop foreign key FK2E0DD0C067676B68 alter table t_list_values drop foreign key FKE01EC98BF4FCB03 alter table t_map_values drop foreign
10、 key FKD169BA107402B585 alter table t_set_values drop foreign key FK7BB8D04A7E79F8BF drop table if exists t_array_value drop table if exists t_collection_mapping drop table if exists t_list_values drop table if exists t_map_values drop table if exists t_set_values create table t_array_value (array_i
11、d integer not null, array_value varchar(255), array_index integer not null, primary key (array_id, array_index) create table t_collection_mapping (id ull auto_increment, name varchar(255), primary key (id) create table t_list_values (list_id integer not null, list_value varchar(255), list_index inte
12、ger not null, primary key (list_id, list_index) create table t_map_values (map_id integer not null, map_value varchar(255), map_key varchar(255) not null, primary key (map_id, map_key) create table t_set_values (set_id integer not null, set_value varchar(255) alter table t_array_value add index FK2E
13、0DD0C067676B68 (array_id), add constraint FK2E0DD0C067676B68 foreign key (array_id) references t_collection_mapping (id) alter table t_list_values add index FKE01EC98BF4FCB03 (list_id), add constraint FKE01EC98BF4FCB03 foreign key (list_id) references t_collection_mapping (id) alter table t_map_valu
14、es add index FKD169BA107402B585 (map_id), add constraint FKD169BA107402B585 foreign key (map_id) references t_collection_mapping (id) alter table t_set_values add index FK7BB8D04A7E79F8BF (set_id), add constraint FK7BB8D04A7E79F8BF foreign key (set_id) references t_collection_mapping (id) 生成的对应的数据库视
15、图如下:二、数据操作1.数据写入写入数据操作,将数据写入时需要注意创建数据对象,其中的List、Set、Map需要创建数据对象,将数据对象写入到数据库中,因为它们三者都是对象接口,所以需要创建一个对象,将对象写入到数据库中,具体代码如下:1234567891011121314151617181920212223242526272829303132333435363738SuppressWarnings( unchecked, rawtypes ) public void testsave() Session session=null; try session=HibernateUtils.ge
16、tSession(); session.beginTransaction(); CollectionMapping cm=new CollectionMapping(); cm.setName(zhangsan); Set set=new HashSet(); set.add(a); set.add(b); cm.setSetValues(set); List list=new ArrayList(); list.add(list1); list.add(list2); cm.setListValues(list); String str=new Stringarray1,array2; cm
17、.setArrayValues(str); Map map=new HashMap(); map.put(k1,v1); map.put(k2, v2); cm.setMapValues(map); session.save(cm); session.getTransaction().commit(); catch(Exception e) e.printStackTrace(); session.getTransaction().rollback(); finally HibernateUtils.closeSession(session); 生成的SQL语句如下:123456789Hibe
18、rnate: insert into t_collection_mapping (name) values (?) Hibernate: insert into t_set_values (set_id, set_value) values (?, ?) Hibernate: insert into t_set_values (set_id, set_value) values (?, ?) Hibernate: insert into t_list_values (list_id, list_index, list_value) values (?, ?, ?) Hibernate: ins
19、ert into t_list_values (list_id, list_index, list_value) values (?, ?, ?) Hibernate: insert into t_map_values (map_id, map_key, map_value) values (?, ?, ?) Hibernate: insert into t_map_values (map_id, map_key, map_value) values (?, ?, ?) Hibernate: insert into t_array_value (array_id, array_index, a
20、rray_value) values (?, ?, ?) Hibernate: insert into t_array_value (array_id, array_index, array_value) values (?, ?, ?) 2.加载数据加载数据的方法很简单,它会将表中的数据按照集合加载到对象中,然后只需要获取相应的对象集合即可。12345678910111213141516171819202122public void testload() Session session=null; try session=HibernateUtils.getSession(); sessio
21、n.beginTransaction(); CollectionMapping cm=(CollectionMapping)session.load(CollectionMapping.class, 1); System.out.println(cm.name= +cm.getName(); System.out.println(cm.list= +cm.getListValues(); System.out.println(cm.map= +cm.getMapValues(); System.out.println(cm.array= +cm.getArrayValues(); System
22、.out.println(cm.set= +cm.getSetValues(); session.getTransaction().commit(); catch(Exception e) e.printStackTrace(); session.getTransaction().rollback(); finally HibernateUtils.closeSession(session); 生成的结果:12345678910Hibernate: select collection0_.id as id0_0_, collection0_.name as name0_0_ from t_co
23、llection_mapping collection0_ where collection0_.id=? Hibernate: select arrayvalue0_.array_id as array1_0_, arrayvalue0_.array_value as array2_0_, arrayvalue0_.array_index as array3_0_ from t_array_value arrayvalue0_ where arrayvalue0_.array_id=? cm.name= zhangsan Hibernate: select listvalues0_.list
24、_id as list1_0_, listvalues0_.list_value as list2_0_, listvalues0_.list_index as list3_0_ from t_list_values listvalues0_ where listvalues0_.list_id=? cm.list= list1, list2 Hibernate: select mapvalues0_.map_id as map1_0_, mapvalues0_.map_value as map2_0_, mapvalues0_.map_key as map3_0_ from t_map_va
25、lues mapvalues0_ where mapvalues0_.map_id=? cm.map= k1=v1, k2=v2 cm.array= Ljava.lang.String;758d8478 Hibernate: select setvalues0_.set_id as set1_0_, setvalues0_.set_value as set2_0_ from t_set_values setvalues0_ where setvalues0_.set_id=? cm.set= b, a 三、总结Hibernate可以持久化以下java集合的实例, 包括java.util.Map
26、, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List, 和任何持久实体或值的数组(使用Set集合类型是最好的选择)。类型为java.util.Collection或者java.util.List的属性还可以使用bag语义来持久。用于持久化的集合,除了集合接口外,不能保留任何实现这些接口的类所附加的语义(例如:LinkedHashSet带来的迭代顺序)。所有的持久化集合,实际上都各自按照 HashMap, HashSet, TreeMap, TreeSet 和 ArrayList 的语义直接工作。更深入地说,对于一个包含集合的属性来说,必须把Java类型定义为接口 (也就是Map, Set 或者List等),而绝不能是HashMap, TreeSet 或者 ArrayList。存在这个限制的原因是,在你不知道的时候,Hibernate暗中把你的Map, Set 和 List 的实例替换成了它自己的关于Map, Set 或者 List 的实现。(所以在你
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1