欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    Hibernate关联关系映射配置 .doc

    • 资源ID:3995668       资源大小:626KB        全文页数:53页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Hibernate关联关系映射配置 .doc

    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七、一对多单向关联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.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;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_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(String name) this.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 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.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) 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;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;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;1.3.4 对象关系映射文件<?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_Husband" table="H_HUSBAND_XML" dynamic-update="true"><id name="id" type="big_decimal"><column name="id" precision="4" 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" column="wife_id" unique="true" not-null="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.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" /><generator class="sequence"><param 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-configuration 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:thin:127.0.0.1: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_session_context_class">thread</property><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><property name="show_sql">true</property><property name="format_sql">true</property><property 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.annotation.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类关联关系图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)GeneratedValue(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 id) this.id = id;public void setName(String name) this.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 Husband 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) 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 实体类package 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 = 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() 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 对象关系映射文件<?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_Husband" table="H_HUSBAND_XML" dynamic-update="true"><id name="id" type="big_decimal"><column name="id" precision="4" 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="true" 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" "<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" /><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="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 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_")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 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 = 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.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.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) 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 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) 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(BigDecimal id) this.id = id;public String getName() return name;public void setName(String name) this.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.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="foreign"><!- 指明具体参考哪一个外键,因为一张表可能存在多个外键 -><param name="property">wife</param></generator></id><property name="name" length="30"></property><one-to-one name="wife" constrained="true"></one-to-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" table="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></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.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Generati

    注意事项

    本文(Hibernate关联关系映射配置 .doc)为本站会员(文库蛋蛋多)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开