@OnetoOne注解
基本属性
//@OneToOne定义如下:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface OneToOne {
Class targetEntity() default void.class;
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
String mappedBy() default "";
}
@OneToOne注释五个属性:targetEntity、cascade、fetch、optional 和mappedBy。
fetch属性默认值是FetchType.EAGER。optional = true设置属性可以为null;
targetEntity属性:Class类型的属性。定义关系类的类型,默认是该成员属性对应的类类型,所以通常不需要提供定义;
cascade属性:CascadeType[]类型。该属性定义类和类之间的级联关系。定义的级联关系将被容器视为对当前类对象及其关联类对象采取相同的操作,而且这种关系是递归调用的。cascade的值只能从CascadeType.PERSIST(级联新建)、CascadeType.REMOVE(级联删除)、CascadeType.REFRESH(级联刷新)、CascadeType.MERGE(级联更新)中选择一个或多个。还有一个选择是使用CascadeType.ALL,表示选择全部四项。
使用方式
- 共享主键
@Entity
public class Address {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
@NotNull
protected String street;
@NotNull
protected String zipcode;
@NotNull
protected String city;
//.....
}
@Entity
@Table(name = "USERS")
public class User {
@Id
protected Long id;
protected String username;
@OneToOne(
fetch = FetchType.LAZY, // Defaults to EAGER
optional = false // Required for lazy loading with proxies!
)
@PrimaryKeyJoinColumn
protected Address shippingAddress;
- 外主键生成器
@Entity
public class Address {
@Id
@GeneratedValue(generator = "addressKeyGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "addressKeyGenerator",
strategy = "foreign",
parameters =
@org.hibernate.annotations.Parameter(
name = "property", value = "user"
)
)
protected Long id;
@NotNull
protected String street;
@NotNull
protected String zipcode;
@NotNull
protected String city;
@OneToOne(optional = false) // Create FK constraint on PK column
@PrimaryKeyJoinColumn
protected User user;
protected Address() {
}
public Address(User user) {
this.user = user;
}
public Address(User user, String street, String zipcode, String city) {
this.user = user;
this.street = street;
this.zipcode = zipcode;
this.city = city;
}
- 外键联结列
@Entity
public class Address {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
@NotNull
protected String street;
@NotNull
protected String zipcode;
@NotNull
protected String city;
}
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
protected String username;
@OneToOne(
fetch = FetchType.LAZY,
optional = false, // NOT NULL
cascade = CascadeType.PERSIST
)
@JoinColumn(unique = true) // Defaults to SHIPPINGADDRESS_ID
protected Address shippingAddress;
- 联结表
@Entity
public class Item {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
protected String name;
}
@Entity
public class Shipment {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
@NotNull
protected Date createdOn = new Date();
@NotNull
protected ShipmentState shipmentState = ShipmentState.TRANSIT;
@OneToOne(fetch = FetchType.LAZY)
@JoinTable(
name = "ITEM_SHIPMENT", // Required!
joinColumns =
@JoinColumn(name = "SHIPMENT_ID"), // Defaults to ID
inverseJoinColumns =
@JoinColumn(name = "ITEM_ID", // Defaults to AUCTION_ID
nullable = false,
unique = true)
)
protected Item auction;
}