ManyToMany (Java EE 5)

Java EE API


javax.persistence Annotation Type ManyToMany


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

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

定义带有多对多多样性的多值关联。如果将 Collection 的元素类型指定为 generic 类型,则不必指定关联的目标实体类;否则必须指定。

每个多对多关联都有两端:拥有端和非拥有端(或反向端)。连接表是对拥有端指定的。如果关联是双向的,则两端都由拥有端指定。

OneToMany 注释的注释元素也适用于 ManyToMany 注释。

示例 1:

在 Customer 类中:

 @ManyToMany
 @JoinTable(name="CUST_PHONES")
 public Set getPhones() { return phones; }

在 PhoneNumber 类中:

@ManyToMany(mappedBy="phones")
public Set getCustomers() { return customers; }
示例 2:

在 Customer 类中:

@ManyToMany(targetEntity=com.acme.PhoneNumber.class)
public Set getPhones() { return phones; }

在 PhoneNumber 类中:

@ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
public Set getCustomers() { return customers; }

示例 3:

在 Customer 类中:

@ManyToMany
    @JoinTable(name="CUST_PHONE",
        joinColumns=
            @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
        inverseJoinColumns=
            @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
        )
    public Set getPhones() { return phones; }

在 PhoneNumberClass 中:

@ManyToMany(mappedBy="phones")
public Set getCustomers() { return customers; }
 
英文文档:

Defines a many-valued association with many-to-many multiplicity. If the Collection is defined using generics to specify the element type, the associated target entity class does not need to be specified; otherwise it must be specified.

Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side.

The same annotation elements for the OneToMany annotation apply to the ManyToMany annotation.

    Example 1:

    In Customer class:

    @ManyToMany
    @JoinTable(name="CUST_PHONES")
    public Set getPhones() { return phones; }

    In PhoneNumber class:

    @ManyToMany(mappedBy="phones")
    public Set getCustomers() { return customers; }

    Example 2:

    In Customer class:

    @ManyToMany(targetEntity=com.acme.PhoneNumber.class)
    public Set getPhones() { return phones; }

    In PhoneNumber class:

    @ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
    public Set getCustomers() { return customers; }

    Example 3:

    In Customer class:

    @ManyToMany
    @JoinTable(name="CUST_PHONE",
        joinColumns=
            @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
        inverseJoinColumns=
            @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
        )
    public Set getPhones() { return phones; }

    In PhoneNumberClass:

    @ManyToMany(mappedBy="phones")
    public Set getCustomers() { return customers; }
 

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