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...................

有时,两个关联实体的主键也可以代替专门的连接字段来表达one-to-one关联,他们的主键是等同的,并且无须专门的链接字段。
@OneToOne(cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
其中@PrimaryKeyJoinColumn
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; 
    } 
} 
在这种关联之中,两个表是直接外键关联,除此以外,还有一种利用关联表的映射: 
association table会建立如下关联表 
create table CUSTOMER_PHONE 
( 
    CUSTOMER_ID int not null, 
    PHONE_ID int not null unique 
); 
需要使用@JoinTable注解进行 
@javax.persistence.JoinTable 

public @interface JoinTable 
{ 
    String name() default ""; 
    String catalog() default ""; 
    String schema() default ""; 
    JoinColumn[] joinColumns() default {}; 
    JoinColumn[] inverseJoinColumns() default {}; 
    UniqueConstraint[] uniqueConstraints() default {}; 
} 
其中:joinColumns() 属性定义的外键,映射到关联关系中作为支配端的一方的主键。inverseJoinColumns()属性则映射到非支配端。如果关联两端的任意一端包括复合主键,我们只要添加多个@JoinColumn注解即可 

应用举例: 
@Entity 
public class Customer implements java.io.Serializable{ 
    ... 
    private Collection<Phone> phoneNumbers = new ArrayList<Phone>(); 
    ... 
    @OneToMany(cascade = {CascadeType.ALL}) 
    @JoinTable(name ="CUSTOMER_PHONE", 
            joinColumns={@JoinColumn(name="CUSTOMER_ID")}, 
            inverseJoinColumns={@JoinColumn(name="Phone_ID")}) 
    public Collection<Phone> getPhoneNumbers(){ 
        return phoneNumbers; 
    } 
    public void setPhoneNumbers(Collection<Phone> phones){ 
        this.phoneNumbers = phones; 
    } 
} 

单向many-to-one 关联 

与one-to-many关联刚好相对应 

@javax.persistence.ManyToOne 
public @interface ManyToOe 
{ 
    Class targetEntity() default void.class; 
    CascadeType[] cascade() default {}; 
    FetchType fetch() default EAGER; //注意:这里ManyToOne默认为EAGER 
    boolean optional() default true; 
} 

应用举例: 
@Entity 
public class Cruise implements java.io.Serializable{ 
    private int id; 
    private String name; 
    private Ship ship; 

    public Curise(){} //默认构造函数:这是JPA规范作要求的 
    public Cruise(String name,Ship ship){ 
        this.name = name; 
        this.ship = ship; 
    } 
    @Id @GeneratedValue 
    public int getId() {return id;} 
    public void setId(int id) {this.id = id;} 

    public String getName() {return name;} 
    public void setName(String name) {this.name = name;} 

    @ManyToOnep 
    @JoinColumn(name="SHIP_ID") 
    public Ship getShip() {return ship;} 
    public void setShip(Shipship) {this.ship = ship;} 
} 

你可能感兴趣的:(Hibernate,jpa)