ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:79.64KB ,
资源ID:8223273      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8223273.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java的Hibernate框架中集合类数据结构的映射编写课案.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java的Hibernate框架中集合类数据结构的映射编写课案.docx

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