JPA @OneToOne @OneToMany @ManyToOne @ManyToMany 描述及说明

JPA 四种关系描述

  • @OneToOne
  • @OneToMany
  • @ManyToOne
  • @ManyToMany

@OneToOne

@Target({METHOD, FIELD}) 
@Retention(RUNTIME)

public @interface OneToOne {

    /** 
     * (Optional) The entity class that is the target of 
     * the association. 
     *
     * 

Defaults to the type of the field or property * that stores the association. */ Class targetEntity() default void.class; /** * (Optional) The operations that must be cascaded to * the target of the association. * *

By default no operations are cascaded. */ CascadeType[] cascade() default {}; /** * (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. */ FetchType fetch() default EAGER; /** * 如果设置为false时,其值必须不能为null * (Optional) Whether the association is optional. If set * to false then a non-null relationship must always exist. */ boolean optional() default true; /** * 拥有mappedBy注解的实体类为关系被维护端 * (Optional) The field that owns the relationship. This * element is only specified on the inverse (non-owning) * side of the association. */ String mappedBy() default ""; /** * (Optional) Whether to apply the remove operation to entities that have * been removed from the relationship and to cascade the remove operation to * those entities. * @since 2.0 */ boolean orphanRemoval() default false; }

@OneToMany

注:

  1. 在JPA规范中,一对多的双向关系由多端来维护,负责关系的增删改查;而一端为关系被维护端,不能维护关系
  2. 关于级联一定要在关系的维护端设置,不能在被维护端设置
@Target({METHOD, FIELD}) 
@Retention(RUNTIME)

public @interface OneToMany {

    /**
     * (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. */ Class targetEntity() default void.class; /** * (Optional) The operations that must be cascaded to * the target of the association. *

Defaults to no operations being cascaded. * *

When the target collection is a {@link java.util.Map * java.util.Map}, the cascade element applies to the * map value. */ CascadeType[] cascade() default {}; /** (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 entities * must be eagerly fetched. The LAZY strategy is a hint to the * persistence provider runtime. */ FetchType fetch() default LAZY; /** * The field that owns the relationship. Required unless * the relationship is unidirectional. */ String mappedBy() default ""; /** * (Optional) Whether to apply the remove operation to entities that have * been removed from the relationship and to cascade the remove operation to * those entities. * @since 2.0 */ boolean orphanRemoval() default false; }

@ManyToOne

@Target({METHOD, FIELD}) 
@Retention(RUNTIME)

public @interface ManyToOne {

    /** 
     * (Optional) The entity class that is the target of 
     * the association. 
     *
     * 

Defaults to the type of the field or property * that stores the association. */ Class targetEntity() default void.class; /** * (Optional) The operations that must be cascaded to * the target of the association. * *

By default no operations are cascaded. */ CascadeType[] cascade() default {}; /** * (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. */ FetchType fetch() default EAGER; /** * (Optional) Whether the association is optional. If set * to false then a non-null relationship must always exist. */ boolean optional() default true; }

@ManyToMany

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

    /**
     * (Optional) The entity class that is the target of the
     * association. Optional only if the collection-valued
     * relationship property is defined using Java generics.  Must be
     * specified otherwise.
     *
     * 

Defaults to the parameterized type of * the collection when defined using generics. */ Class targetEntity() default void.class; /** * (Optional) The operations that must be cascaded to the target * of the association. * *

When the target collection is a {@link java.util.Map * java.util.Map}, the cascade element applies to the * map value. * *

Defaults to no operations being cascaded. */ CascadeType[] cascade() default {}; /** (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 entities * must be eagerly fetched. The LAZY strategy is a hint to the * persistence provider runtime. */ FetchType fetch() default LAZY; /** * The field that owns the relationship. Required unless * the relationship is unidirectional. */ String mappedBy() default ""; }

你可能感兴趣的:(Spring,Java,java,spring)