⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example-mappings.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;24&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#22797;&#26434;&#26144;&#23556;&#23454;&#20363;</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 - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="up" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="previous" href="example-weblog.html" title="&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;"><link rel="next" href="best-practices.html" title="&#31532;&nbsp;25&nbsp;&#31456;&nbsp;&#26368;&#20339;&#23454;&#36341;(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">&#31532;&nbsp;24&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#22797;&#26434;&#26144;&#23556;&#23454;&#20363;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="example-weblog.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="best-practices.html">&#19979;&#19968;&#39029;</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>&#31532;&nbsp;24&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#22797;&#26434;&#26144;&#23556;&#23454;&#20363;</h2></div></div><div></div></div><p>        &#26412;&#31456;&#23637;&#31034;&#20102;&#19968;&#20123;&#36739;&#20026;&#22797;&#26434;&#30340;&#20851;&#31995;&#26144;&#23556;&#12290;    </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.&nbsp;Employer&#65288;&#38599;&#20027;)/Employee(&#38599;&#21592;)</h2></div></div><div></div></div><p>            &#19979;&#38754;&#20851;&#20110;<tt class="literal">Employer</tt> &#21644; <tt class="literal">Employee</tt>&#30340;&#20851;&#31995;&#27169;&#22411;&#20351;&#29992;&#20102;&#19968;&#20010;&#30495;&#23454;&#30340;&#23454;&#20307;&#31867;            (<tt class="literal">Employment</tt>)&#26469;&#34920;&#36848;&#65292;&#36825;&#26159;&#22240;&#20026;&#23545;&#20110;&#30456;&#21516;&#30340;&#38599;&#21592;&#21644;&#38599;&#20027;&#21487;&#33021;&#20250;&#26377;&#22810;&#20010;&#38599;&#20323;&#26102;&#38388;&#27573;&#12290;            &#23545;&#20110;&#37329;&#39069;&#21644;&#38599;&#21592;&#22995;&#21517;&#65292;&#29992;Components&#24314;&#27169;&#12290;                    </p><div class="mediaobject" align="center"><img src="../shared/images/EmployerEmployee.gif" align="middle"></div><p>            &#26144;&#23556;&#25991;&#20214;&#21487;&#33021;&#26159;&#36825;&#26679;:        </p><pre class="programlisting">&lt;hibernate-mapping&gt;            &lt;class name="Employer" table="employers"&gt;        &lt;id name="id"&gt;            &lt;generator class="sequence"&gt;                &lt;param name="sequence"&gt;employer_id_seq&lt;/param&gt;            &lt;/generator&gt;        &lt;/id&gt;        &lt;property name="name"/&gt;    &lt;/class&gt;    &lt;class name="Employment" table="employment_periods"&gt;        &lt;id name="id"&gt;            &lt;generator class="sequence"&gt;                &lt;param name="sequence"&gt;employment_id_seq&lt;/param&gt;            &lt;/generator&gt;        &lt;/id&gt;        &lt;property name="startDate" column="start_date"/&gt;        &lt;property name="endDate" column="end_date"/&gt;        &lt;component name="hourlyRate" class="MonetaryAmount"&gt;            &lt;property name="amount"&gt;                &lt;column name="hourly_rate" sql-type="NUMERIC(12, 2)"/&gt;            &lt;/property&gt;            &lt;property name="currency" length="12"/&gt;        &lt;/component&gt;        &lt;many-to-one name="employer" column="employer_id" not-null="true"/&gt;        &lt;many-to-one name="employee" column="employee_id" not-null="true"/&gt;    &lt;/class&gt;    &lt;class name="Employee" table="employees"&gt;        &lt;id name="id"&gt;            &lt;generator class="sequence"&gt;                &lt;param name="sequence"&gt;employee_id_seq&lt;/param&gt;            &lt;/generator&gt;        &lt;/id&gt;        &lt;property name="taxfileNumber"/&gt;        &lt;component name="name" class="Name"&gt;            &lt;property name="firstName"/&gt;            &lt;property name="initial"/&gt;            &lt;property name="lastName"/&gt;        &lt;/component&gt;    &lt;/class&gt;&lt;/hibernate-mapping&gt;</pre><p>        &#29992;<tt class="literal">SchemaExport</tt>&#29983;&#25104;&#34920;&#32467;&#26500;&#12290;    </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.&nbsp;Author(&#20316;&#23478;)/Work(&#20316;&#21697;)</h2></div></div><div></div></div><p>                        &#32771;&#34385;&#19979;&#38754;&#30340;<tt class="literal">Work</tt>,<tt class="literal">Author</tt> &#21644; <tt class="literal">Person</tt>&#27169;&#22411;&#30340;&#20851;&#31995;&#12290;            &#25105;&#20204;&#29992;&#22810;&#23545;&#22810;&#20851;&#31995;&#26469;&#25551;&#36848;<tt class="literal">Work</tt> &#21644; <tt class="literal">Author</tt>&#65292;            &#29992;&#19968;&#23545;&#19968;&#20851;&#31995;&#26469;&#25551;&#36848;<tt class="literal">Author</tt> &#21644; <tt class="literal">Person</tt>&#65292;            &#21478;&#19968;&#31181;&#21487;&#33021;&#24615;&#26159;<tt class="literal">Author</tt>&#32487;&#25215;<tt class="literal">Person</tt>&#12290;        </p><div class="mediaobject" align="center"><img src="../shared/images/AuthorWork.gif" align="middle"></div><p>            &#19979;&#38754;&#30340;&#26144;&#23556;&#25991;&#20214;&#27491;&#30830;&#30340;&#25551;&#36848;&#20102;&#36825;&#20123;&#20851;&#31995;&#65306;        </p><pre class="programlisting">&lt;hibernate-mapping&gt;    &lt;class name="Work" table="works" discriminator-value="W"&gt;        &lt;id name="id" column="id"&gt;            &lt;generator class="native"/&gt;        &lt;/id&gt;        &lt;discriminator column="type" type="character"/&gt;        &lt;property name="title"/&gt;        &lt;set name="authors" table="author_work"&gt;            &lt;key column name="work_id"/&gt;            &lt;many-to-many class="Author" column name="author_id"/&gt;        &lt;/set&gt;        &lt;subclass name="Book" discriminator-value="B"&gt;            &lt;property name="text"/&gt;        &lt;/subclass&gt;        &lt;subclass name="Song" discriminator-value="S"&gt;            &lt;property name="tempo"/&gt;            &lt;property name="genre"/&gt;        &lt;/subclass&gt;    &lt;/class&gt;    &lt;class name="Author" table="authors"&gt;        &lt;id name="id" column="id"&gt;            &lt;!-- The Author must have the same identifier as the Person --&gt;            &lt;generator class="assigned"/&gt;         &lt;/id&gt;        &lt;property name="alias"/&gt;        &lt;one-to-one name="person" constrained="true"/&gt;        &lt;set name="works" table="author_work" inverse="true"&gt;            &lt;key column="author_id"/&gt;            &lt;many-to-many class="Work" column="work_id"/&gt;        &lt;/set&gt;    &lt;/class&gt;    &lt;class name="Person" table="persons"&gt;        &lt;id name="id" column="id"&gt;            &lt;generator class="native"/&gt;        &lt;/id&gt;        &lt;property name="name"/&gt;    &lt;/class&gt;&lt;/hibernate-mapping&gt;</pre><p>                &#26144;&#23556;&#20013;&#26377;4&#20010;&#34920;&#12290;<tt class="literal">works</tt>, <tt class="literal">authors</tt> &#21644; <tt class="literal">persons</tt>        &#20998;&#21035;&#20445;&#23384;&#30528;work&#65292;author&#21644;person&#30340;&#25968;&#25454;&#12290;<tt class="literal">author_work</tt>&#26159;authors&#21644;works&#30340;&#20851;&#32852;&#34920;&#12290;        &#34920;&#32467;&#26500;&#26159;&#30001;<tt class="literal">SchemaExport</tt>&#29983;&#25104;&#30340;&#12290;    </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.&nbsp;Customer(&#23458;&#25143;)/Order(&#35746;&#21333;)/Product(&#20135;&#21697;)</h2></div></div><div></div></div><p>                        &#29616;&#22312;&#26469;&#32771;&#34385;<tt class="literal">Customer</tt>,<tt class="literal">Order</tt> &#65292; <tt class="literal">LineItem</tt>            &#21644; <tt class="literal">Product</tt>&#20851;&#31995;&#30340;&#27169;&#22411;&#12290;<tt class="literal">Customer</tt> &#21644; <tt class="literal">Order</tt>&#20043;&#38388;            &#26159;&#19968;&#23545;&#22810;&#30340;&#20851;&#31995;&#65292;&#20294;&#26159;&#25105;&#20204;&#24590;&#20040;&#26469;&#25551;&#36848;<tt class="literal">Order</tt> / <tt class="literal">LineItem</tt> / <tt class="literal">Product</tt>&#21602;&#65311;            &#25105;&#21487;&#20197;&#25226;<tt class="literal">LineItem</tt>&#20316;&#20026;&#25551;&#36848;<tt class="literal">Order</tt> &#21644; <tt class="literal">Product</tt>            &#22810;&#23545;&#22810;&#20851;&#31995;&#30340;&#20851;&#32852;&#31867;&#65292;&#22312;Hibernate&#65292;&#36825;&#21483;&#20570;&#32452;&#21512;&#20803;&#32032;&#12290;        </p><div class="mediaobject" align="center"><img src="../shared/images/CustomerOrderProduct.gif" align="middle"></div><p>            &#26144;&#23556;&#25991;&#20214;&#22914;&#19979;&#65306;        </p><pre class="programlisting">&lt;hibernate-mapping&gt;    &lt;class name="Customer" table="customers"&gt;        &lt;id name="id"&gt;            &lt;generator class="native"/&gt;        &lt;/id&gt;        &lt;property name="name"/&gt;        &lt;set name="orders" inverse="true"&gt;            &lt;key column="customer_id"/&gt;            &lt;one-to-many class="Order"/&gt;        &lt;/set&gt;    &lt;/class&gt;    &lt;class name="Order" table="orders"&gt;        &lt;id name="id"&gt;            &lt;generator class="native"/&gt;        &lt;/id&gt;        &lt;property name="date"/&gt;        &lt;many-to-one name="customer" column="customer_id"/&gt;        &lt;list name="lineItems" table="line_items"&gt;            &lt;key column="order_id"/&gt;            &lt;list-index column="line_number"/&gt;            &lt;composite-element class="LineItem"&gt;                &lt;property name="quantity"/&gt;                &lt;many-to-one name="product" column="product_id"/&gt;            &lt;/composite-element&gt;        &lt;/list&gt;    &lt;/class&gt;    &lt;class name="Product" table="products"&gt;        &lt;id name="id"&gt;            &lt;generator class="native"/&gt;        &lt;/id&gt;        &lt;property name="serialNumber"/&gt;    &lt;/class&gt;&lt;/hibernate-mapping&gt;</pre><p>        <tt class="literal">customers</tt>, <tt class="literal">orders</tt>, <tt class="literal">line_items</tt> &#21644;         <tt class="literal">products</tt> &#20998;&#21035;&#20445;&#23384;&#30528;customer, order, order line item &#21644; product&#30340;&#25968;&#25454;&#12290;        <tt class="literal">line_items</tt>&#20063;&#20316;&#20026;&#36830;&#25509;orders &#21644; products&#30340;&#20851;&#32852;&#34920;&#12290;    </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 + -