版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、hibernate关联关系映射配置2一、一对一单向外键关联:21.1目录结构21.2 annotation方式21.3 xml方式41.4 hibernate配置文件7二、一对一双向外键关联72.1 annotation方式72.2 xml方式9三、一对一单向主键关联(不重要)123.1 annotation方式123.2 xml方式14四、一对一双向主键关联(不重要)164.1 annotation方式163.2 xml方式19五、组件映射215.1 annotation方式215.2 xml方式23六、多对一单向关联256.1 annotation方式256.2 xml方式27七、一对多单
2、向关联297.1 annotation方式297.2 xml方式31八、一对多、多对一双向关联348.1 annotation方式348.2 xml方式36九、多对多单向关联398.1 annotation方式398.2 xml方式41十、多对多双向关联448.1 annotation方式448.2 xml方式46hibernate关联关系映射配置一、 一对一单向外键关联:1.1目录结构图1-1 目录结构图1.2 annotation方式1.2.1 类图图1-2 类关联关系图1.2.2数据库表结构图1-3数据库表结构图1.2.3 实体类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")/不写也没问题,自动生成的字段也叫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 类图图1-4 类关联关系图1.3.2 数据库表结构图1-5数据库表结构图1.3.3 实体类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 对象关系映射文件<?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是站在当前类xml_husband的角度来看,xml_husband与wife是多对一-> </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>二、一对一双向外键关联2.1 annotation方式2.1.1 类图图2-1类
22、关联关系图2.1.2 数据库结构图图2-2数据库表结构图2.1.3 实体类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")/不写也没问题,自动生成的字段也叫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 类图图2-3 类关联关系图2.2.2 数据库结构图2.2.3 实体类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 对象关系映射文件<?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类中的属性名,表示这里的关联关系已经由husband的对象映射文件的wife属性上定义过了 -><one-to-one name
35、="husband" property-ref="wife" /> </class></hibernate-mapping>三、一对一单向主键关联(不重要)3.1 annotation方式3.1.1 类图图3-1 类关联关系图3.1.2 数据库表结构图3-2 数据库表结构图3.1.3 实体类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;注:只生成了表,但是没有外键约束,具体实现还需要查找3.2 xml方式3.2.1 类图图3-3 类关联关系图3.2.2 数据库表结构图3-4 数据库表结构图3.2.3 实体类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 对象映射文件<?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"><!- 指明具体参考哪一个外键,因为一张表可能存在多个外键 -><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>四、一对一双向主键关联(不重要)4.1 annotation方式4.1.1 类图图4-1 类关联关系图4.1.2 数据库表结构图4-2 数据库表结构图4.1.3 实体类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;注:同样不产生关联关系,具体实现待查3.2 xml方式4.2.1 类图图4-3 类关联关系图4.2.2 数据库表结构图4-4 数据库表结构图4.2.3 实体类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 对象映射文件<?xml version="1.0"?><!doctype hibernate-mapping public "-/hibernate/hibernate mapping dtd 3.0/en" "<hibernate-mapping
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合同 仲裁诉讼条款
- 大班音乐绘本《月光长廊》课件
- 2024上海市非定期集装箱道路货物运输合同
- 三年级语文上册第一单元测试卷-基础知识与综合能力篇 含答案 部编版
- 2024家庭水电装修合同书
- 2024收银员聘用合同
- 2024标准销售代理合同格式
- 深圳大学《哲学经典与人生》2021-2022学年第一学期期末试卷
- 深圳大学《形体训练(流行舞蹈)》2022-2023学年第一学期期末试卷
- 合同样本-土建合同范本8篇
- 2024年江苏省中等职业学校学生学业水平考试机械CAD绘图试卷(含5张图)
- 2023年中国铁路国际有限公司招聘考试试题及答案
- 沪科版(2024)八年级全一册物理第一学期期中学业质量测试卷(含答案)
- 计算机图形学智慧树知到期末考试答案章节答案2024年北京理工大学
- 2024年山东省港口集团有限公司招聘笔试参考题库含答案解析
- 30屈原《楚辞·橘颂》课件
- 《学生仪容仪表》主题班会PPT课件
- 国民经济统计学 第3章中间消耗及投入产出核算
- 课程设计(论文)3kta梨果酱车间工艺设计
- 毕业设计(论文)长沙办公楼空调系统设计
- 第三章电阻材料
评论
0/150
提交评论