📄 associations.html
字号:
</class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )create table Address ( addressId bigint not null primary key ) </pre></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="assoc-bidirectional"></a>8.4. 双向关联(Bidirectional associations)</h2></div></div><div></div></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="assoc-bidirectional-m21"></a>8.4.1. 一对多(one to many) / 多对一(many to one)</h3></div></div><div></div></div><p> <span class="emphasis"><em>双向多对一关联</em></span> 是最常见的关联关系。(这也是标准的父/子关联关系。) </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" not-null="true"/></class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <set name="people" inverse="true"> <key column="addressId"/> <one-to-many class="Person"/> </set></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key, addressId bigint not null )create table Address ( addressId bigint not null primary key ) </pre></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="assoc-bidirectional-121"></a>8.4.2. 一对一(one to one)</h3></div></div><div></div></div><p> <span class="emphasis"><em>基于外键关联的双向一对一关联</em></span>也很常见。 </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/></class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <one-to-one name="person" property-ref="address"/></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key, addressId bigint not null unique )create table Address ( addressId bigint not null primary key ) </pre><p> <span class="emphasis"><em>基于主键关联的一对一关联</em></span>需要使用特定的id生成器。 </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <one-to-one name="address"/></class><class name="Address"> <id name="id" column="personId"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" constrained="true"/></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key )create table Address ( personId bigint not null primary key ) </pre></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="assoc-bidirectional-join"></a>8.5. 使用连接表的双向关联(Bidirectional associations with join tables)</h2></div></div><div></div></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="assoc-bidirectional-join-12m"></a>8.5.1. 一对多(one to many) /多对一( many to one)</h3></div></div><div></div></div><p> <span class="emphasis"><em>基于连接表的双向一对多关联</em></span>。注意<tt class="literal">inverse="true"</tt>可以出现在关联的任意一端,即collection端或者join端。 </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses" table="PersonAddress"> <key column="personId"/> <many-to-many column="addressId" unique="true" class="Address"/> </set></class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <join table="PersonAddress" inverse="true" optional="true"> <key column="addressId"/> <many-to-one name="person" column="personId" not-null="true"/> </join></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )create table Address ( addressId bigint not null primary key ) </pre></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="assoc-bidirectional-join-121"></a>8.5.2. 一对一(one to one)</h3></div></div><div></div></div><p> <span class="emphasis"><em>基于连接表的双向一对一关联</em></span>极为罕见,但也是可行的。 </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <join table="PersonAddress" optional="true"> <key column="personId" unique="true"/> <many-to-one name="address" column="addressId" not-null="true" unique="true"/> </join></class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <join table="PersonAddress" optional="true" inverse="true"> <key column="addressId" unique="true"/> <many-to-one name="address" column="personId" not-null="true" unique="true"/> </join></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )create table Address ( addressId bigint not null primary key ) </pre></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="assoc-bidirectional-join-m2m"></a>8.5.3. 多对多(many to many)</h3></div></div><div></div></div><p> 最后,还有 <span class="emphasis"><em>双向多对多关联</em></span>. </p><pre class="programlisting"><class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses"> <key column="personId"/> <many-to-many column="addressId" class="Address"/> </set></class><class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <set name="people" inverse="true"> <key column="addressId"/> <many-to-many column="personId" class="Person"/> </set></class></pre><pre class="programlisting">create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )create table Address ( addressId bigint not null primary key ) </pre></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="collections.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="components.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">第 7 章 集合类(Collections)映射 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 第 9 章 组件(Component)映射</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -