📄 example-mappings.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>第 18 章 示例:不同的映射</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - 符合Java习惯的关系数据库持久化"><link rel="up" href="index.html" title="HIBERNATE - 符合Java习惯的关系数据库持久化"><link rel="previous" href="example-weblog.html" title="第 17 章 示例:Weblog 应用程序"><link rel="next" href="best-practices.html" title="第 19 章 最佳实践(Best Practices)"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">第 18 章 示例:不同的映射</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="example-weblog.html">上一页</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="best-practices.html">下一页</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="example-mappings"></a>第 18 章 示例:不同的映射</h2></div></div><div></div></div><p> 本章将为你展示几个比较复杂的关联映射。 </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-mappings-emp"></a>18.1. 雇员/雇主(Employer/Employee)</h2></div></div><div></div></div><p> 接下来关于<tt class="literal">Employer</tt>和<tt class="literal">Employee</tt>关系的模型使用了一个实体(entity)类(<tt class="literal">Employment</tt>)来表示这个关联,因为对于相同的雇主和雇员可能会有多个雇用时间段。对于雇用金额和雇员姓名,我们使用组件(component)来进行建模。 </p><div class="mediaobject" align="center"><img src="../shared/images/EmployerEmployee.gif" align="middle"></div><p> 这是一个可行的映射文档: </p><pre class="programlisting"><hibernate-mapping> <class name="Employer" table="employers"> <id name="id"> <generator class="sequence"> <param name="sequence">employer_id_seq</param> </generator> </id> <property name="name"/> </class> <class name="Employment" table="employment_periods"> <id name="id"> <generator class="sequence"> <param name="sequence">employment_id_seq</param> </generator> </id> <property name="startDate" column="start_date"/> <property name="endDate" column="end_date"/> <component name="hourlyRate" class="MonetoryAmount"> <property name="amount"> <column name="hourly_rate" sql-type="NUMERIC(12, 2)"/> </property> <property name="currency" length="12"/> </component> <many-to-one name="employer" column="employer_id" not-null="true"/> <many-to-one name="employee" column="employee_id" not-null="true"/> </class> <class name="Employee" table="employees"> <id name="id"> <generator class="sequence"> <param name="sequence">employee_id_seq</param> </generator> </id> <property name="taxfileNumber"/> <component name="name" class="Name"> <property name="firstName"/> <property name="initial"/> <property name="lastName"/> </component> </class></hibernate-mapping></pre><p> 这里是由<tt class="literal">SchemaExport</tt>生成的表结构。 </p><pre class="programlisting">create table employers ( id BIGINT not null, name VARCHAR(255), primary key (id))create table employment_periods ( id BIGINT not null, hourly_rate NUMERIC(12, 2), currency VARCHAR(12), employee_id BIGINT not null, employer_id BIGINT not null, end_date TIMESTAMP, start_date TIMESTAMP, primary key (id))create table employees ( id BIGINT not null, firstName VARCHAR(255), initial CHAR(1), lastName VARCHAR(255), taxfileNumber VARCHAR(255), primary key (id))alter table employment_periods add constraint employment_periodsFK0 foreign key (employer_id) references employersalter table employment_periods add constraint employment_periodsFK1 foreign key (employee_id) references employeescreate sequence employee_id_seqcreate sequence employment_id_seqcreate sequence employer_id_seq</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-mappings-authorwork"></a>18.2. 作者/著作(Author/Work)</h2></div></div><div></div></div><p> 下面的例子是关于<tt class="literal">Work</tt>、<tt class="literal">Author</tt>和<tt class="literal">Person</tt>。我们用多对多关系来表示<tt class="literal">Work</tt>和<tt class="literal">Author</tt>之间的关联,用一对一的关系来表示<tt class="literal">Author</tt>和<tt class="literal">Person</tt>之间的关联。另外一种可行的方式是对<tt class="literal">Author</tt>扩展<tt class="literal">Person</tt>。 </p><div class="mediaobject" align="center"><img src="../shared/images/AuthorWork.gif" align="middle"></div><p> 接下来的映射文档正确地表示这些关系: </p><pre class="programlisting"><hibernate-mapping> <class name="Work" table="works" discriminator-value="W"> <id name="id" column="id"> <generator class="native"/> </id> <discriminator column="type" type="character"/> <property name="title"/> <set name="authors" table="author_work" lazy="true"> <key> <column name="work_id" not-null="true"/> </key> <many-to-many class="Author"> <column name="author_id" not-null="true"/> </many-to-many> </set> <subclass name="Book" discriminator-value="B"> <property name="text"/> </subclass> <subclass name="Song" discriminator-value="S"> <property name="tempo"/> <property name="genre"/> </subclass> </class> <class name="Author" table="authors"> <id name="id" column="id"> <!-- The Author must have the same identifier as the Person --> <generator class="assigned"/> </id>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -