📄 example-mappings.html
字号:
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>第 24 章 示例:复杂映射实例</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="第 23 章 示例:Weblog 应用程序"><link rel="next" href="best-practices.html" title="第 25 章 最佳实践(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">第 24 章 示例:复杂映射实例</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>第 24 章 示例:复杂映射实例</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>24.1. Employer(雇主)/Employee(雇员)</h2></div></div><div></div></div><p> 下面关于<tt class="literal">Employer</tt> 和 <tt class="literal">Employee</tt>的关系模型使用了一个真实的实体类 (<tt class="literal">Employment</tt>)来表述,这是因为对于相同的雇员和雇主可能会有多个雇佣时间段。 对于金额和雇员姓名,用Components建模。 </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="MonetaryAmount"> <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>24.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"> <key column name="work_id"/> <many-to-many class="Author" column name="author_id"/> </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> <property name="alias"/> <one-to-one name="person" constrained="true"/> <set name="works" table="author_work" inverse="true"> <key column="author_id"/> <many-to-many class="Work" column="work_id"/> </set> </class> <class name="Person" table="persons"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> </class></hibernate-mapping></pre><p> 映射中有4个表。<tt class="literal">works</tt>, <tt class="literal">authors</tt> 和 <tt class="literal">persons</tt> 分别保存着work,author和person的数据。<tt class="literal">author_work</tt>是authors和works的关联表。 表结构是由<tt class="literal">SchemaExport</tt>生成的。 </p><pre class="programlisting">create table works ( id BIGINT not null generated by default as identity, tempo FLOAT, genre VARCHAR(255), text INTEGER, title VARCHAR(255), type CHAR(1) not null, primary key (id))create table author_work ( author_id BIGINT not null, work_id BIGINT not null, primary key (work_id, author_id))create table authors ( id BIGINT not null generated by default as identity, alias VARCHAR(255), primary key (id))create table persons ( id BIGINT not null generated by default as identity, name VARCHAR(255), primary key (id))alter table authors add constraint authorsFK0 foreign key (id) references personsalter table author_work add constraint author_workFK0 foreign key (author_id) references authorsalter table author_work add constraint author_workFK1 foreign key (work_id) references works</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-mappings-customerorderproduct"></a>24.3. Customer(客户)/Order(订单)/Product(产品)</h2></div></div><div></div></div><p> 现在来考虑<tt class="literal">Customer</tt>,<tt class="literal">Order</tt> , <tt class="literal">LineItem</tt> 和 <tt class="literal">Product</tt>关系的模型。<tt class="literal">Customer</tt> 和 <tt class="literal">Order</tt>之间 是一对多的关系,但是我们怎么来描述<tt class="literal">Order</tt> / <tt class="literal">LineItem</tt> / <tt class="literal">Product</tt>呢? 我可以把<tt class="literal">LineItem</tt>作为描述<tt class="literal">Order</tt> 和 <tt class="literal">Product</tt> 多对多关系的关联类,在Hibernate,这叫做组合元素。 </p><div class="mediaobject" align="center"><img src="../shared/images/CustomerOrderProduct.gif" align="middle"></div><p> 映射文件如下: </p><pre class="programlisting"><hibernate-mapping> <class name="Customer" table="customers"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="orders" inverse="true"> <key column="customer_id"/> <one-to-many class="Order"/> </set> </class> <class name="Order" table="orders"> <id name="id"> <generator class="native"/> </id> <property name="date"/> <many-to-one name="customer" column="customer_id"/> <list name="lineItems" table="line_items"> <key column="order_id"/> <list-index column="line_number"/> <composite-element class="LineItem"> <property name="quantity"/> <many-to-one name="product" column="product_id"/> </composite-element> </list> </class> <class name="Product" table="products"> <id name="id"> <generator class="native"/> </id> <property name="serialNumber"/> </class></hibernate-mapping></pre><p> <tt class="literal">customers</tt>, <tt class="literal">orders</tt>, <tt class="literal">line_items</tt> 和 <tt class="literal">products</tt> 分别保存着customer, order, order line item 和 product的数据。 <tt class="literal">line_items</tt>也作为连接orders 和 products的关联表。 </p><pre class="programlisting">create table customers ( id BIGINT not null generated by default as identity, name VARCHAR(255), primary key (id))create table orders ( id BIGINT not null generated by default as identity, customer_id BIGINT, date TIMESTAMP, primary key (id))create table line_items ( line_number INTEGER not null, order_id BIGINT not null, product_id BIGINT,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -