OneToMany (Java EE 5)

Java EE API


javax.persistence Annotation Type OneToMany


@Target(value={METHOD,FIELD})
@Retention(value=RUNTIME)
public @interface OneToMany

Implements: Annotation
@Target(value={METHOD, FIELD})
@Retention(value=RUNTIME)

定义带有一对多多样性的多值关联。

如果将 Collection 的元素类型指定为 generic 类型,则不必指定关联的目标实体类型;否则必须指定目标实体类。

示例 1:使用 generic 的一对多关联

在 Customer 类中:

    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set getOrders() { return orders; }

在 Order 类中:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }

示例 2:没有使用 generic 的一对多关联

在 Customer 类中:

    @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
            mappedBy="customer")
    public Set getOrders() { return orders; }

在 Order 类中:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }
 
英文文档:

Defines a many-valued association with one-to-many multiplicity.

If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified.

    Example 1: One-to-Many association using generics

    In Customer class:

    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set getOrders() { return orders; }

    In Order class:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }

    Example 2: One-to-Many association without using generics

    In Customer class:

    @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
            mappedBy="customer")
    public Set getOrders() { return orders; }

    In Order class:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }
 

Since:
Java Persistence 1.0

Optional Element Summary
 CascadeType[]
 FetchType
 String
 Class
 

abstract public Class<T> targetEntity()
(可选)作为关联目标的实体类。仅当使用 Java generic 定义了 Collection 属性时才是可选的。否则必须指定。

使用 generic 定义时,默认为参数化的 Collection 类型。

英文文档:

targetEntity

public abstract Class targetEntity
(Optional) The entity class that is the target of the association. Optional only if the collection property is defined using Java generics. Must be specified otherwise.

Defaults to the parameterized type of the collection when defined using generics.

Default:
void.class

abstract public CascadeType[] cascade()
(可选)必须串联到关联目标的操作。

默认为不串联任何操作。

英文文档:

cascade

public abstract CascadeType[] cascade
(Optional) The operations that must be cascaded to the target of the association.

Defaults to no operations being cascaded.

Default:
{}

abstract public FetchType fetch()
可选)关联应该延迟加载还是必须立即获取。EAGER 战略是必须立即获取关联实体的持久性提供者运行时的要求。LAZY 战略是持久性提供者运行时的提示。
英文文档:

fetch

public abstract FetchType fetch
(Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistenceprovider runtime that the associatedentities must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.

Default:
LAZY

abstract public String mappedBy()
拥有关系的字段。只要关系不是单向的,就需要它。
英文文档:

mappedBy

public abstract String mappedBy
The field that owns the relationship. Required unless the relationship is unidirectional.

Default:
""


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!