Hibernate Annotation

单项one-to-one关联
@OneToOne

public @interface OneToOne
{
    Class targetEntity() default void.class;
    CascadeType[] cascade() default{};
    FetchType fetch() default EAGER;
    boolean optional() default true;
    String mappedBy() default "";
}

 
关联方法:
1..................

@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "ADDRESS_ID")
当使用复合主键的时候用@JoinColumns注解,如果不是主键则用referencedColumnName = ?
其中对于@JoinColumn

public @interface JoinColumn
{
    String name() default "";
    String referencedColumnName() default "";
    boolean unique() default false;
    boolean nullable() default true;
    boolean insertable() default true;
    boolean updatable() default true;
    String columnDefinition() default "";
    String table() default "";
}

 其中对于@JoinColumns

public @interface JoinColumns
{
    JoinColumn[] value();
}

 
2...................

public @interface PrimaryKeyJoinColumn
{
    String name() default "";
    String referencedColumnName() default "";
    String columnDefinition() default "";
}

 如果是复合主键就用@PrimaryKeyJoinColumns

public @interface PrimaryKeyJoinColumns
{
    PrimaryKeyJoinColumn[] value();
}

 

单向One-to-Many关联 

public @interface OneToMany 
{ 
    Class targetEntity() default void.class; 
    CascadeType[] cascade() default {}; 
    FetchType fetch() default LAZY; 
    String mappedBy() default ""; 
} 

 特别注意:这里fetch的默认属性为 LAZY 

@Entity 
public class Customer implements java.io.Serializable{ 
    ... 
    private Collection<Phone> phoneNumbers = new ArrayList<Phone>(); 
    ... 
    @OneToMany(cascade = {CascadeType.ALL}) 
    @JoinColumn(name = "CUSTOMER_ID") 
    public Collection<Phone> getPhoneNumbers(){ 
        return phoneNumbers; 
    } 
    public void setPhoneNumbers(Collection<Phone> phones){ 
        this.phoneNumbers = phones; 
    } 
} 

 

你可能感兴趣的:(Hibernate)