OneToOne (Java EE 5)

Java EE API


javax.persistence Annotation Type OneToOne


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

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

此注释定义对另一个带有一对一多样性的实体的单值关联。一般不必显式指定关联的目标实体,因为通常可以根据引用的对象类型推断出来。
示例 1:映射外键列的一对一关联

在 Customer 类中:

    @OneToOne(optional=false)
    @JoinColumn(
    	name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
    public CustomerRecord getCustomerRecord() { return customerRecord; }

在 CustomerRecord 类中:

    @OneToOne(optional=false, mappedBy="customerRecord")
    public Customer getCustomer() { return customer; }

示例 2:假定源和目标共享同一个主键值的一对一关联。 

在 Employee 类中:

    @Entity
    public class Employee {
    	@Id Integer id;
    
    	@OneToOne @PrimaryKeyJoinColumn
    	EmployeeInfo info;
    	...
    }

对于 EmployeeInfo 类:

    @Entity
    public class EmployeeInfo {
    	@Id Integer id;
    	...
    }
 
英文文档:

This annotation defines a single-valued association to another entity that has one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced.

    Example 1: One-to-one association that maps a foreign key column

    On Customer class:

    @OneToOne(optional=false)
    @JoinColumn(
        name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
    public CustomerRecord getCustomerRecord() { return customerRecord; }

    On CustomerRecord class:

    @OneToOne(optional=false, mappedBy="customerRecord")
    public Customer getCustomer() { return customer; }

    Example 2: One-to-one association that assumes both the source and target share the same primary key values. 

    On Employee class:

    @Entity
    public class Employee {
        @Id Integer id;
    
        @OneToOne @PrimaryKeyJoinColumn
        EmployeeInfo info;
        ...
    }

    On EmployeeInfo class:

    @Entity
    public class EmployeeInfo {
        @Id Integer id;
        ...
    }
 

Since:
Java Persistence 1.0

Optional Element Summary
 CascadeType[]
 FetchType
 String
 boolean
 Class
 

abstract public Class<T> targetEntity()
(可选)作为关联目标的实体类。

默认为存储关联的字段或属性的类型。

英文文档:

targetEntity

public abstract Class targetEntity
(Optional) The entity class that is the target of the association.

Defaults to the type of the field or property that stores the association.

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.

By default no operations are 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 persistence provider runtime that the associated entity must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.

Default:
EAGER

abstract public boolean optional()
(可选)关联是否是可选的。如果设置为 false,则必须总是存在非 null 关系。
英文文档:

optional

public abstract boolean optional
(Optional) Whether the association is optional. If set to false then a non-null relationship must always exist.

Default:
true

abstract public String mappedBy()
(可选)拥有关系的字段。此元素仅在关联的反向端(非拥有端)指定。
英文文档:

mappedBy

public abstract String mappedBy
(Optional) The field that owns the relationship. This element is only specified on the inverse (non-owning) side of the association.

Default:
""


Submit a bug or feature

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

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

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