《Hibernate关联关系映射配置 .doc》由会员分享,可在线阅读,更多相关《Hibernate关联关系映射配置 .doc(53页珍藏版)》请在三一办公上搜索。
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, generator=husband_seq_gene)public Big
5、Decimal 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(String name) this.name = name;public void setWife(Wife wife) thi
6、s.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 javax.persistence.Id;import javax.persistence.SequenceGener
7、ator;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.SEQUENCE, generator=wife_seq_gene)public BigDecimal getId() return id;public void setId(BigDeci
8、mal id) this.id = id;Column(length=30)public String getName() return name;public void setName(String name) this.name = name;1.3 XML方式1.3.1 类图图1-4 类关联关系图1.3.2 数据库表结构图1-5数据库表结构图1.3.3 实体类package com.rongqq.hibernate3.xml.entity;import java.math.BigDecimal;public class XML_Husband private BigDecimal id;
9、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) this.name = name;public void setWife(XML_Wife wife) this.wife = wife;
10、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(BigDecimal id) this.id = id;public String getName() return name;public void setName(String name) this.name = name
11、;1.3.4 对象关系映射文件!DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/ENH_STUDENT_SEQ_XML !DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN H_STUDENT_SEQ_XML 1.4 Hibernate配置文件!DOCTYPE hibernate-configuration PUBLIC -/Hibernate/Hibernate Configuration DTD 3.0
12、/EN oracle.jdbc.driver.OracleDriverjdbc:oracle:thin:127.0.0.1:1521:silencecnusercn8303061threadorg.hibernate.cache.NoCacheProvidertruetrueorg.hibernate.dialect.Oracle9Dialect!-update-二、一对一双向外键关联2.1 Annotation方式2.1.1 类图图2-1类关联关系图2.1.2 数据库结构图图2-2数据库表结构图2.1.3 实体类Entity(name=H_HUSBAND_)SequenceGenerator
13、(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 BigDecimal getId() return id;Column(len
14、gth=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(String name) this.name = name;public void setWife(Wife wife) this.wife = wife;Entity(name=H_WIFE_)Se
15、quenceGenerator(name=wife_seq_gene, sequenceName=WIFE_SEQ)public class Wife private BigDecimal id;private String name;private Husband husband;IdColumn(precision=4)GeneratedValue(strategy=GenerationType.SEQUENCE, generator=wife_seq_gene)public BigDecimal getId() return id;public void setId(BigDecimal
16、 id) this.id = id;Column(length=30)public String getName() return name;public void setName(String name) this.name = 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 实体类p
17、ackage 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)
18、this.id = id;public void setName(String name) this.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;private XML_Husband husband;public BigDecimal getId(
19、) return id;public void setId(BigDecimal id) this.id = id;public String getName() return name;public void setName(String name) this.name = name;public XML_Husband getHusband() return husband;public void setHusband(XML_Husband husband) this.husband = husband;2.2.4 对象关系映射文件!DOCTYPE hibernate-mapping P
20、UBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN H_STUDENT_SEQ_XML !DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN H_STUDENT_SEQ_XML 三、一对一单向主键关联(不重要)3.1 Annotation方式3.1.1 类图图3-1 类关联关系图3.1.2 数据库表结构图3-2 数据库表结构图3.1.3 实体类package com.rongqq.hibernate3.annotation.entity;import jav
21、a.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.PrimaryKeyJoinColumn;import javax.persistence.Seque
22、nceGenerator;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)GeneratedValue(strategy=GenerationType.SEQUENCE, generator=husband_se
23、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) this.name = name;public void setWife(Wife wife) this.wife = wif
24、e;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.SequenceGenerator;Entity(
25、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.SEQUENCE, generator=wife_seq_gene)public BigDecimal getId() return id;public void setId(BigDecimal id) this
26、.id = id;Column(length=30)public String getName() return name;public void setName(String name) this.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.BigDecimal;public class XML_Husband private
27、 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) this.name = name;public void setWife(XML_Wife wife) th
28、is.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(BigDecimal id) this.id = id;public String g
29、etName() return name;public void setName(String name) this.name = name;3.2.4 对象映射文件!DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN wife !DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 3.0/EN H_STUDENT_SEQ_XML 四、一对一双向主键关联(不重要)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.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Generati
链接地址:https://www.31ppt.com/p-3995668.html