Hibernate關(guān)聯(lián)關(guān)系映射配置_第1頁(yè)
Hibernate關(guān)聯(lián)關(guān)系映射配置_第2頁(yè)
Hibernate關(guān)聯(lián)關(guān)系映射配置_第3頁(yè)
Hibernate關(guān)聯(lián)關(guān)系映射配置_第4頁(yè)
Hibernate關(guān)聯(lián)關(guān)系映射配置_第5頁(yè)
已閱讀5頁(yè),還剩48頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、hibernate關(guān)聯(lián)關(guān)系映射配置2一、一對(duì)一單向外鍵關(guān)聯(lián):21.1目錄結(jié)構(gòu)21.2 annotation方式21.3 xml方式41.4 hibernate配置文件7二、一對(duì)一雙向外鍵關(guān)聯(lián)72.1 annotation方式72.2 xml方式9三、一對(duì)一單向主鍵關(guān)聯(lián)(不重要)123.1 annotation方式123.2 xml方式14四、一對(duì)一雙向主鍵關(guān)聯(lián)(不重要)164.1 annotation方式163.2 xml方式19五、組件映射215.1 annotation方式215.2 xml方式23六、多對(duì)一單向關(guān)聯(lián)256.1 annotation方式256.2 xml方式27七、一對(duì)多單

2、向關(guān)聯(lián)297.1 annotation方式297.2 xml方式31八、一對(duì)多、多對(duì)一雙向關(guān)聯(lián)348.1 annotation方式348.2 xml方式36九、多對(duì)多單向關(guān)聯(lián)398.1 annotation方式398.2 xml方式41十、多對(duì)多雙向關(guān)聯(lián)448.1 annotation方式448.2 xml方式46hibernate關(guān)聯(lián)關(guān)系映射配置一、 一對(duì)一單向外鍵關(guān)聯(lián):1.1目錄結(jié)構(gòu)圖1-1 目錄結(jié)構(gòu)圖1.2 annotation方式1.2.1 類(lèi)圖圖1-2 類(lèi)關(guān)聯(lián)關(guān)系圖1.2.2數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖1-3數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖1.2.3 實(shí)體類(lèi)package com.rongqq.hibernate3

3、.annotation.entity;import java.math.bigdecimal;import javax.persistence.column;import javax.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.onetoone;import javax.persistence.sequencegenerator;enti

4、ty(name="h_husband_")sequencegenerator(name="husband_seq_gene", sequencename="husband_seq", allocationsize=1)public class husband private bigdecimal id;private string name;private wife wife;idcolumn(precision=4, scale=0)generatedvalue(strategy=generationtype.sequence, g

5、enerator="husband_seq_gene")public bigdecimal getid() return id;column(length=30)public string getname() return name;onetoone/joincolumn(name="wife_id")/不寫(xiě)也沒(méi)問(wèn)題,自動(dòng)生成的字段也叫wife_idpublic wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(stri

6、ng name) = name;public void setwife(wife wife) this.wife = wife;package com.rongqq.hibernate3.annotation.entity;import java.math.bigdecimal;import javax.persistence.column;import javax.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import

7、 javax.persistence.id;import javax.persistence.sequencegenerator;entity(name="h_wife_")sequencegenerator(name="wife_seq_gene", sequencename="wife_seq")public class wife private bigdecimal id;private string name;idcolumn(precision=4)generatedvalue(strategy=generationtype

8、.sequence, generator="wife_seq_gene")public bigdecimal getid() return id;public void setid(bigdecimal id) this.id = id;column(length=30)public string getname() return name;public void setname(string name) = name;1.3 xml方式1.3.1 類(lèi)圖圖1-4 類(lèi)關(guān)聯(lián)關(guān)系圖1.3.2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖1-5數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖1.3.3 實(shí)體類(lèi)package

9、com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_husband private bigdecimal id;private string name;private xml_wife wife;public bigdecimal getid() return id;public string getname() return name;public xml_wife getwife() return wife;public void setid(bigdecimal id) this.id

10、 = id;public void setname(string name) = name;public void setwife(xml_wife wife) this.wife = wife;package com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_wife private bigdecimal id;private string name;public bigdecimal getid() return id;public void setid(bigde

11、cimal id) this.id = id;public string getname() return name;public void setname(string name) = name;1.3.4 對(duì)象關(guān)系映射文件<?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en""<hibernate-mapping package="com.rong

12、qq.hibernate3.xml.entity"><class name="xml_husband" table="h_husband_xml" dynamic-update="true"><id name="id" type="big_decimal"><column name="id" precision="4" scale="0" /><generator class=&

13、quot;sequence"><param name="sequence">h_student_seq_xml</param></generator></id><property name="name" length="30"></property><many-to-one name="wife" column="wife_id" unique="true" not-null=&qu

14、ot;true" /><!-many-to-one是站在當(dāng)前類(lèi)xml_husband的角度來(lái)看,xml_husband與wife是多對(duì)一-> </class></hibernate-mapping><?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" "<hibernate-mapping package="com.r

15、ongqq.hibernate3.xml.entity"><class name="xml_wife" table="h_wife_xml" dynamic-update="true"><id name="id"><column name="id" precision="4" scale="0" /><generator class="sequence"><param

16、 name="sequence">h_student_seq_xml</param></generator></id><property name="name" length="30" /> </class></hibernate-mapping>1.4 hibernate配置文件<?xml version='1.0' encoding='utf-8'?><!doctype hibernate-configu

17、ration public "-/hibernate/hibernate configuration dtd 3.0/en" "<hibernate-configuration><session-factory><property name="connection.driver_class">oracle.jdbc.driver.oracledriver</property><property name="connection.url">jdbc:oracle:th

18、in::1521:silence</property><property name="connection.username">cnuser</property><property name="connection.password">cn830306</property><property name="connection.pool_size">1</property><property name="current_

19、session_context_class">thread</property><property name="vider_class">org.hibernate.cache.nocacheprovider</property><property name="show_sql">true</property><property name="format_sql">true</property><property

20、 name="dialect">org.hibernate.dialect.oracle9dialect</property><!-<property name="hbm2ddl.auto">update</property>-><mapping class="com.rongqq.hibernate3.annotation.entity.husband" /><mapping class="com.rongqq.hibernate3.annotatio

21、n.entity.wife" /><mapping resource="com/rongqq/hibernate3/xml/entity/xml_husband.hbm.xml" /><mapping resource="com/rongqq/hibernate3/xml/entity/xml_wife.hbm.xml" /></session-factory></hibernate-configuration>二、一對(duì)一雙向外鍵關(guān)聯(lián)2.1 annotation方式2.1.1 類(lèi)圖圖2-1類(lèi)

22、關(guān)聯(lián)關(guān)系圖2.1.2 數(shù)據(jù)庫(kù)結(jié)構(gòu)圖圖2-2數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖2.1.3 實(shí)體類(lèi)entity(name="h_husband_")sequencegenerator(name="husband_seq_gene", sequencename="husband_seq", allocationsize=1)public class husband private bigdecimal id;private string name;private wife wife;idcolumn(precision=4, scale=0)generated

23、value(strategy=generationtype.sequence, generator="husband_seq_gene")public bigdecimal getid() return id;column(length=30)public string getname() return name;onetoone/joincolumn(name="wife_id")/不寫(xiě)也沒(méi)問(wèn)題,自動(dòng)生成的字段也叫wife_idpublic wife getwife() return wife;public void setid(bigdecimal

24、id) this.id = id;public void setname(string name) = name;public void setwife(wife wife) this.wife = wife;entity(name="h_wife_")sequencegenerator(name="wife_seq_gene", sequencename="wife_seq")public class wife private bigdecimal id;private string name;private h

25、usband husband;idcolumn(precision=4)generatedvalue(strategy=generationtype.sequence, generator="wife_seq_gene")public bigdecimal getid() return id;public void setid(bigdecimal id) this.id = id;column(length=30)public string getname() return name;public void setname(string name) =

26、 name;onetoone(mappedby="wife")public husband gethusband() return husband;public void sethusband(husband husband) this.husband = husband;2.2 xml方式2.2.1 類(lèi)圖圖2-3 類(lèi)關(guān)聯(lián)關(guān)系圖2.2.2 數(shù)據(jù)庫(kù)結(jié)構(gòu)圖2.2.3 實(shí)體類(lèi)package com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_husband private bi

27、gdecimal id;private string name;private xml_wife wife;public bigdecimal getid() return id;public string getname() return name;public xml_wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(string name) = name;public void setwife(xml_wife wife) this.

28、wife = wife;package com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_wife private bigdecimal id;private string name;private xml_husband husband;public bigdecimal getid() return id;public void setid(bigdecimal id) this.id = id;public string getname() return name;public vo

29、id setname(string name) = name;public xml_husband gethusband() return husband;public void sethusband(xml_husband husband) this.husband = husband;2.2.4 對(duì)象關(guān)系映射文件<?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" &qu

30、ot;<hibernate-mapping package="com.rongqq.hibernate3.xml.entity"><class name="xml_husband" table="h_husband_xml" dynamic-update="true"><id name="id" type="big_decimal"><column name="id" precision="4"

31、 scale="0" /><generator class="sequence"><param name="sequence">h_student_seq_xml</param></generator></id><property name="name" length="30"></property><many-to-one name="wife" unique="tru

32、e" not-null="true" ><column name="wife_id" precision="4"></column></many-to-one> </class></hibernate-mapping><?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en&q

33、uot; "<hibernate-mapping package="com.rongqq.hibernate3.xml.entity"><class name="xml_wife" table="h_wife_xml" dynamic-update="true"><id name="id"><column name="id" precision="4" scale="0" />&

34、lt;generator class="sequence"><param name="sequence">h_student_seq_xml</param></generator></id><property name="name" length="30" /><!- 這里的wife是xml_husband類(lèi)中的屬性名,表示這里的關(guān)聯(lián)關(guān)系已經(jīng)由husband的對(duì)象映射文件的wife屬性上定義過(guò)了 -><one-to-one name

35、="husband" property-ref="wife" /> </class></hibernate-mapping>三、一對(duì)一單向主鍵關(guān)聯(lián)(不重要)3.1 annotation方式3.1.1 類(lèi)圖圖3-1 類(lèi)關(guān)聯(lián)關(guān)系圖3.1.2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖3-2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖3.1.3 實(shí)體類(lèi)package com.rongqq.hibernate3.annotation.entity;import java.math.bigdecimal;import javax.persistence.column;import java

36、x.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.onetoone;import javax.persistence.primarykeyjoincolumn;import javax.persistence.sequencegenerator;entity(name="h_husband_")sequencegener

37、ator(name="husband_seq_gene", sequencename="husband_seq", allocationsize=1)public class husband private bigdecimal id;private string name;private wife wife;idcolumn(precision=4, scale=0)generatedvalue(strategy=generationtype.sequence, generator="husband_seq_gene")public

38、 bigdecimal getid() return id;column(length=30)public string getname() return name;onetooneprimarykeyjoincolumnpublic wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(string name) = name;public void setwife(wife wife) this.wife = wife;package com

39、.rongqq.hibernate3.annotation.entity;import java.math.bigdecimal;import javax.persistence.column;import javax.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.sequencegenerator;entity(name="h_

40、wife_")sequencegenerator(name="wife_seq_gene", sequencename="wife_seq")public class wife private bigdecimal id;private string name;idcolumn(precision=4)generatedvalue(strategy=generationtype.sequence, generator="wife_seq_gene")public bigdecimal getid() return id;pu

41、blic void setid(bigdecimal id) this.id = id;column(length=30)public string getname() return name;public void setname(string name) = name;注:只生成了表,但是沒(méi)有外鍵約束,具體實(shí)現(xiàn)還需要查找3.2 xml方式3.2.1 類(lèi)圖圖3-3 類(lèi)關(guān)聯(lián)關(guān)系圖3.2.2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖3-4 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖3.2.3 實(shí)體類(lèi)package com.rongqq.hibernate3.xml.entity;import java.math.bigdecim

42、al;public class xml_husband private bigdecimal id;private string name;private xml_wife wife;public bigdecimal getid() return id;public string getname() return name;public xml_wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(string name) = name;pu

43、blic void setwife(xml_wife wife) this.wife = wife;package com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;import com.rongqq.hibernate3.annotation.entity.husband;public class xml_wife private bigdecimal id;private string name;public bigdecimal getid() return id;public void setid(bigdecim

44、al id) this.id = id;public string getname() return name;public void setname(string name) = name;3.2.4 對(duì)象映射文件<?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" "<hibernate-mapping package="com.rongqq.h

45、ibernate3.xml.entity"><class name="xml_husband" table="h_husband_xml" dynamic-update="true"><id name="id" type="big_decimal"><column name="id" precision="4" scale="0" /><generator class="

46、;foreign"><!- 指明具體參考哪一個(gè)外鍵,因?yàn)橐粡埍砜赡艽嬖诙鄠€(gè)外鍵 -><param name="property">wife</param></generator></id><property name="name" length="30"></property><one-to-one name="wife" constrained="true"></one-to-

47、one> </class></hibernate-mapping><?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" "<hibernate-mapping package="com.rongqq.hibernate3.xml.entity"><class name="xml_wife" tabl

48、e="h_wife_xml" dynamic-update="true"><id name="id"><column name="id" precision="4" scale="0" /><generator class="sequence"><param name="sequence">h_student_seq_xml</param></generator&g

49、t;</id><property name="name" length="30" /> </class></hibernate-mapping>四、一對(duì)一雙向主鍵關(guān)聯(lián)(不重要)4.1 annotation方式4.1.1 類(lèi)圖圖4-1 類(lèi)關(guān)聯(lián)關(guān)系圖4.1.2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖4-2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖4.1.3 實(shí)體類(lèi)package com.rongqq.hibernate3.annotation.entity;import java.math.bigdecimal;import javax.persistence

50、.column;import javax.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.onetoone;import javax.persistence.primarykeyjoincolumn;import javax.persistence.sequencegenerator;entity(name="h_husband_&

51、quot;)sequencegenerator(name="husband_seq_gene", sequencename="husband_seq", allocationsize=1)public class husband private bigdecimal id;private string name;private wife wife;idcolumn(precision=4, scale=0)generatedvalue(strategy=generationtype.sequence, generator="husband_se

52、q_gene")public bigdecimal getid() return id;column(length=30)public string getname() return name;onetooneprimarykeyjoincolumnpublic wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(string name) = name;public void setwife(wife wife) this.wife

53、 = wife;package com.rongqq.hibernate3.annotation.entity;import java.math.bigdecimal;import javax.persistence.column;import javax.persistence.entity;import javax.persistence.generatedvalue;import javax.persistence.generationtype;import javax.persistence.id;import javax.persistence.onetoone;import jav

54、ax.persistence.primarykeyjoincolumn;import javax.persistence.sequencegenerator;entity(name="h_wife_")sequencegenerator(name="wife_seq_gene", sequencename="wife_seq")public class wife private bigdecimal id;private string name;private husband husband;idcolumn(precision=4)

55、generatedvalue(strategy=generationtype.sequence, generator="wife_seq_gene")public bigdecimal getid() return id;public void setid(bigdecimal id) this.id = id;column(length=30)public string getname() return name;public void setname(string name) = name;onetooneprimarykeyjoincolumnpu

56、blic husband gethusband() return husband;public void sethusband(husband husband) this.husband = husband;注:同樣不產(chǎn)生關(guān)聯(lián)關(guān)系,具體實(shí)現(xiàn)待查3.2 xml方式4.2.1 類(lèi)圖圖4-3 類(lèi)關(guān)聯(lián)關(guān)系圖4.2.2 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖4-4 數(shù)據(jù)庫(kù)表結(jié)構(gòu)圖4.2.3 實(shí)體類(lèi)package com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_husband private bigdecimal id;p

57、rivate string name;private xml_wife wife;public bigdecimal getid() return id;public string getname() return name;public xml_wife getwife() return wife;public void setid(bigdecimal id) this.id = id;public void setname(string name) = name;public void setwife(xml_wife wife) this.wife = wife;p

58、ackage com.rongqq.hibernate3.xml.entity;import java.math.bigdecimal;public class xml_wife private bigdecimal id;private string name;private xml_husband husband;public bigdecimal getid() return id;public void setid(bigdecimal id) this.id = id;public string getname() return name;public void setname(st

59、ring name) = name;public xml_husband gethusband() return husband;public void sethusband(xml_husband husband) this.husband = husband;4.2.4 對(duì)象映射文件<?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" "<hibernate-mapping

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論