此内容没有您所选择的语言版本。

7.2.5. One-to-many associations


A one-to-many association links the tables of two classes via a foreign key with no intervening collection table. This mapping loses certain semantics of normal Java collections:
  • An instance of the contained entity class cannot belong to more than one instance of the collection.
  • An instance of the contained entity class cannot appear at more than one value of the collection index.
An association from Product to Part requires the existence of a foreign key column and possibly an index column to the Part table. A <one-to-many> tag indicates that this is a one-to-many association.
<one-to-many 
        class="ClassName"                                   
        not-found="ignore|exception"                        
        entity-name="EntityName"                            
        node="element-name"
        embed-xml="true|false"
    />
Copy to Clipboard Toggle word wrap

1

class (required): the name of the associated class.

2

not-found (optional - defaults to exception): specifies how cached identifiers that reference missing rows will be handled. ignore will treat a missing row as a null association.

3

entity-name (optional): the entity name of the associated class, as an alternative to class.
The <one-to-many> element does not need to declare any columns. Nor is it necessary to specify the table name anywhere.

Warning

If the foreign key column of a <one-to-many> association is declared NOT NULL, you must declare the <key> mapping not-null="true" or use a bidirectional association with the collection mapping marked inverse="true". See the discussion of bidirectional associations later in this chapter for more information.
The following example shows a map of Part entities by name, where partName is a persistent property of Part. Notice the use of a formula-based index:
<map name="parts"
        cascade="all">
    <key column="productId" not-null="true"/>
    <map-key formula="partName"/>
    <one-to-many class="Part"/>
</map>
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat