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