一對一單向關聯樣本:
·客戶實體類CustomerEO:
@Entity
@Table(name="customer") //主表
public class CustomerEO implement Serializable{
@Id
@GeneratedValue(stragegy=GenerationType=AUTO)
private int id;
@OneToOne(casade={CascadeTypeType.ALL})
@JoinColumn(name="address_id")//關聯的表為address表,其主鍵是id
private AddressEO address;
....
}
·地址薄類AddressEO:
@Entity
@Table(name="address")//從表
public class AddressEO implement Serializable{
@Id
@GeneratedValue(stragegy=GenerationType=AUTO)
private int id;
....//在單項關聯中,從表對應的類中不需要引用主表類
}
·雙向關聯(Bidirectional)
如上述樣本中,將AddressEO修改如下:
·地址薄類AddressEO:
@Entity
@Table(name="address")
public class AddressEO implement Serializable{
@Id
@GeneratedValue(stragegy=GenerationType=AUTO)
private int id;