📄 collections.html
字号:
<index-many-to-many column="employee_id" class="Employee"/> <one-to-many column="contract_id" class="Contract"/></map></pre><pre class="programlisting"><map name="connections" lazy="true"> <key column="node1_id"/> <index-many-to-many column="node2_id" class="Node"/> <many-to-many column="connection_id" class="Connection"/></map></pre></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="collections-s1-11b"></a>6.11. 异类关联(Heterogeneous Associations)</h2></div></div><div></div></div><p> <tt class="literal"><many-to-any></tt>和<tt class="literal"><index-many-to-any></tt>元素提供真正的异类关联。这些元素和<tt class="literal"><any></tt>元素工作方式是同样的,他们都应该很少用到。 </p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="collections-s1-12"></a>6.12. 集合例子(Collection Example)</h2></div></div><div></div></div><p> 在前面的几个章节的确非常令人迷惑。 因此让我们来看一个例子。这个类: </p><pre class="programlisting">package eg;import java.util.Set;public class Parent { private long id; private Set children; public long getId() { return id; } private void setId(long id) { this.id=id; } private Set getChildren() { return children; } private void setChildren(Set children) { this.children=children; } .... ....}</pre><p> 这个类有一个<tt class="literal">eg.Child</tt>的实例集合。如果每一个子实例至多有一个父实例, 那么最自然的映射是一个one-to-many的关联关系: </p><pre class="programlisting"><hibernate-mapping> <class name="eg.Parent"> <id name="id"> <generator class="sequence"/> </id> <set name="children" lazy="true"> <key column="parent_id"/> <one-to-many class="eg.Child"/> </set> </class> <class name="eg.Child"> <id name="id"> <generator class="sequence"/> </id> <property name="name"/> </class></hibernate-mapping></pre><p> 在以下的表定义中反应了这个映射关系: </p><pre class="programlisting">create table parent ( id bigint not null primary key )create table child ( id bigint not null primary key, name varchar(255), parent_id bigint )alter table child add constraint childfk0 (parent_id) references parent</pre><p> 如果父亲是<span class="emphasis"><em>必须</em></span>的, 那么就可以使用双向one-to-many的关联了(请看后面父/子关系的章节)。 </p><pre class="programlisting"><hibernate-mapping> <class name="eg.Parent"> <id name="id"> <generator class="sequence"/> </id> <set name="children" inverse="true" lazy="true"> <key column="parent_id"/> <one-to-many class="eg.Child"/> </set> </class> <class name="eg.Child"> <id name="id"> <generator class="sequence"/> </id> <property name="name"/> <many-to-one name="parent" class="eg.Parent" column="parent_id" not-null="true"/> </class></hibernate-mapping></pre><p> 请注意<tt class="literal">NOT NULL</tt>的约束: </p><pre class="programlisting">create table parent ( id bigint not null primary key )create table child ( id bigint not null primary key, name varchar(255), parent_id bigint not null )alter table child add constraint childfk0 (parent_id) references parent</pre><p> 另外一方面,如果一个子实例可能有多个父实例, 那么就
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -