![Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/19/8dbe879c-ea63-4b0b-95bd-16f5497db054/8dbe879c-ea63-4b0b-95bd-16f5497db0541.gif)
![Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程_第2頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/19/8dbe879c-ea63-4b0b-95bd-16f5497db054/8dbe879c-ea63-4b0b-95bd-16f5497db0542.gif)
![Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程_第3頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/19/8dbe879c-ea63-4b0b-95bd-16f5497db054/8dbe879c-ea63-4b0b-95bd-16f5497db0543.gif)
![Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程_第4頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/19/8dbe879c-ea63-4b0b-95bd-16f5497db054/8dbe879c-ea63-4b0b-95bd-16f5497db0544.gif)
![Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程_第5頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/19/8dbe879c-ea63-4b0b-95bd-16f5497db054/8dbe879c-ea63-4b0b-95bd-16f5497db0545.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程Hibernate可以將Java中幾個內(nèi)置的集合結(jié)構(gòu)映射為數(shù)據(jù)庫使用的關(guān)系模型,下面我們就來看一下Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程:一、集合映射1.集合小介集合映射也是基本的映射,但在開發(fā)過程中不會經(jīng)常用到,所以不需要深刻了解,只需要理解基本的使用方法即可,等在開發(fā)過程中遇到了這種問題時能夠查詢到解決方法就可以了。對應(yīng)集合映射它其實是指將java中的集合映射到對應(yīng)的表中,是一種集合對象的映射,在java中有四種類型的集合,分別是Set、Map、List還有普通的數(shù)組,它們之間有很大的區(qū)別:(1)Set,
2、不可以有重復(fù)的對象,對象是無序的;(2)List,可以與重復(fù)的對象,對象之間有順序;(3)Map,它是鍵值成對出現(xiàn)的;(4)數(shù)組,可以重復(fù),對象之間有順序。它們之間的區(qū)別決定了在開發(fā)時使用哪種集合,通常在開發(fā)時會使用Set,它內(nèi)部的對象是無需的,并可以使用迭代器獲取內(nèi)部對象。這幾種集合想要映射到相應(yīng)的關(guān)系模型的話就必須使用Hibernate提供的映射標簽,<set>、<list>、<map>、<array>。2.映射小介繼續(xù)討論集合映射的關(guān)系模型,集合映射是指一個對象對應(yīng)著另一個對象集合,在保存時Hibernate會把數(shù)據(jù)集合保存到相應(yīng)的表中,并
3、按照自己分配的id把數(shù)據(jù)保存到數(shù)據(jù)表中,如果單獨為集合分配了新表,那么會將id分配給集合表的id,那么對應(yīng)的關(guān)系表如下圖:3.類文件集合映射是如何通過代碼實現(xiàn)的,接下來具體分析。這里把所有的集合封存到一個類中,這個類我們稱之為CollectionMapping.java,那么它對應(yīng)的內(nèi)部代碼如下:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263package com.hibernate; import java
4、.util.List; import 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(St
5、ring name) = name; /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.l
6、istValues = listValues; /數(shù)組集合 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
7、) this.mapValues = mapValues; 該類中封裝了幾種常用的集合,想要轉(zhuǎn)化為關(guān)系模型,就必須來看下文的映射。4.集合映射集合的映射其實相當(dāng)?shù)暮唵?,只需要添加對?yīng)的集合標簽,Hibernate分別提供了集合標簽<set>、<map>、<list>、<array>,通過使用集中標簽來將集合映射為對應(yīng)的關(guān)系表,另外通過添加<key>標簽來實現(xiàn)表外鍵的關(guān)聯(lián),其它的屬性通過使用<element>來添加。CollectionMapping.hbm.xml123456789101112131415161718192
8、0212223242526272829303132<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-/Hibernate/Hibernate Mapping DTD 3.0/EN" " <hibernate-mapping> <class name="com.hibernate.CollectionMapping" table="t_collection_mapping"> <id name
9、="id"> <generator class="native"/> </id> <property name="name"/> <set name="setValues" table="t_set_values"> <key column="set_id"></key> <element type="string" column="set_value"
10、></element> </set> <list name="listValues" table="t_list_values"> <key column="list_id"/> <list-index column="list_index"/> <element type="string" column="list_value"/> </list> <map name="
11、;mapValues" table="t_map_values"> <key column="map_id"/> <map-key type="string" column="map_key"/> <element type="string" column="map_value"/> </map> <array name="arrayValues" table="t_array
12、_value"> <key column="array_id"/> <index column="array_index" type="integer"></index> <element type="string" column="array_value"/> </array> </class> </hibernate-mapping> 需要注意的是list標簽和array標簽,這兩種集合內(nèi)的對
13、象是有順序的,所以在添加映射標簽時需要使用list-index或者index標簽來標明對象的順序,而且在添加子標簽時一定要按照順序添加,也就是說先添加<key>標簽,后添加<list-index>標簽,最后添加<element>標簽,否則的話會出現(xiàn)如下錯誤:The content of element type "list" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(index|list-index),(element|one-to-many|
14、many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".5.關(guān)系模型將配置好的對象模型轉(zhuǎn)化為相應(yīng)的關(guān)系模型,生成的SQL語句如下:123456789101112131415161718alter table t_array_value drop foreign key FK2E0DD0C067676B68 alter table t_list_values drop foreign key FKE01EC98BF4
15、FCB03 alter table t_map_values drop foreign 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
16、_values create table t_array_value (array_id 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 nul
17、l, list_value varchar(255), list_index integer 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(25
18、5) alter table t_array_value add index FK2E0DD0C067676B68 (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_col
19、lection_mapping (id) alter table t_map_values 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) refe
20、rences t_collection_mapping (id) 生成的對應(yīng)的數(shù)據(jù)庫視圖如下:二、數(shù)據(jù)操作1.數(shù)據(jù)寫入寫入數(shù)據(jù)操作,將數(shù)據(jù)寫入時需要注意創(chuàng)建數(shù)據(jù)對象,其中的List、Set、Map需要創(chuàng)建數(shù)據(jù)對象,將數(shù)據(jù)對象寫入到數(shù)據(jù)庫中,因為它們?nèi)叨际菍ο蠼涌?,所以需要?chuàng)建一個對象,將對象寫入到數(shù)據(jù)庫中,具體代碼如下:1234567891011121314151617181920212223242526272829303132333435363738SuppressWarnings( "unchecked", "rawtypes" ) public
21、void testsave() Session session=null; try session=HibernateUtils.getSession(); 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 ArrayL
22、ist(); list.add("list1"); list.add("list2"); cm.setListValues(list); String str=new String"array1","array2" cm.setArrayValues(str); Map map=new HashMap(); map.put("k1","v1"); map.put("k2", "v2"); cm.setMapValues(map); se
23、ssion.save(cm); session.getTransaction().commit(); catch(Exception e) e.printStackTrace(); session.getTransaction().rollback(); finally HibernateUtils.closeSession(session); 生成的SQL語句如下:123456789Hibernate: insert into t_collection_mapping (name) values (?) Hibernate: insert into t_set_values (set_id,
24、 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: insert into t_list_values (list_id, list_index, list_value) values (?, ?, ?) Hibernate: insert into t_map
25、_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, array_value) values (?, ?, ?) Hibernate: insert into t_array_value (array_id, array_index, array_value)
26、 values (?, ?, ?) 2.加載數(shù)據(jù)加載數(shù)據(jù)的方法很簡單,它會將表中的數(shù)據(jù)按照集合加載到對象中,然后只需要獲取相應(yīng)的對象集合即可。12345678910111213141516171819202122public void testload() Session session=null; try session=HibernateUtils.getSession(); session.beginTransaction(); CollectionMapping cm=(CollectionMapping)session.load(CollectionMapping.class, 1)
27、; System.out.println("= "+cm.getName(); System.out.println("cm.list= "+cm.getListValues(); System.out.println("cm.map= "+cm.getMapValues(); System.out.println("cm.array= "+cm.getArrayValues(); System.out.println("cm.set= "+cm.getSetValues(); s
28、ession.getTransaction().commit(); catch(Exception e) e.printStackTrace(); session.getTransaction().rollback(); finally HibernateUtils.closeSession(session); 生成的結(jié)果:12345678910Hibernate: select collection0_.id as id0_0_, collection0_.name as name0_0_ from t_collection_mapping collection0_ where collec
29、tion0_.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=? = zhangsan Hibernate: select listvalues0_.list_id as list1_0_, listvalues0_.list_value a
30、s 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_values mapvalues0_ where mapvalues0_.map_id=
31、? 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 三、總結(jié)Hibernate可以持久化以下java集合的實例, 包括java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List, 和任何持久實體或值的數(shù)組(使用Set集合類型是
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 溝通與協(xié)調(diào)打造和諧職場環(huán)境
- 生態(tài)建筑引領(lǐng)未來商業(yè)趨勢
- 現(xiàn)代科技在股票市場分析中的應(yīng)用
- 校園餐飲消費大數(shù)據(jù)洞察學(xué)生消費習(xí)慣
- 2024年八年級生物下冊 6.2.1遺傳說課稿 (新版)冀教版
- 2024年八年級物理下冊 8.1認識壓強說課稿 (新版)粵教滬版
- 14《普羅米修斯》(說課稿)2024-2025學(xué)年-統(tǒng)編版語文四年級上冊
- 2024年五年級數(shù)學(xué)下冊 五 分數(shù)除法練習(xí)五說課稿 北師大版
- 2024-2025學(xué)年高中歷史 專題1 中國傳統(tǒng)文化主流思想的演變 3 宋明理學(xué)說課稿 人民版必修3
- 2024-2025學(xué)年八年級物理下冊 第十章 從粒子到宇宙 10.1 認識分子說課稿 (新版)粵教滬版
- 冀教版小學(xué)英語六年級下冊全冊教案
- 2024人工智能開源大模型生態(tài)體系研究報告
- 2024年中考語文復(fù)習(xí)分類必刷:非連續(xù)性文本閱讀(含答案解析)
- 緊密型縣域醫(yī)療衛(wèi)生共同體慢病管理中心運行指南試行等15個指南
- YYT 0681.11-2014 無菌醫(yī)療器械包裝試驗方法 第11部分:目力檢測醫(yī)用包裝密封完整性
- 遼寧省沈陽市第七中學(xué)2023-2024學(xué)年七年級下學(xué)期期末數(shù)學(xué)試題
- 2024年湖南工業(yè)職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫附答案
- 快速入門穿越機-讓你迅速懂穿越機
- 水利安全生產(chǎn)風(fēng)險防控“六項機制”右江模式經(jīng)驗分享
- 2024年四川省成都市高新區(qū)中考數(shù)學(xué)二診試卷
- 幼兒園衛(wèi)生保健開學(xué)培訓(xùn)
評論
0/150
提交評論