组件
他们没有自己的标识符,其主键是它所属实体拥有映射的数据库标识符,内嵌的组件有独立的生命周期:在保存所拥有的实体实例时.也会保存该组件实例,当其所拥有实体实例被删除时,也会删除该组件实例,Hibernate甚至不必为此执行任何特殊的SQL,所有数据都位于一行中.
Java没有组合的概念,类或属性不能被标记为组件或组合生命周期,与实体的唯一区别是数据库标识符:组件类没有独立的标识;因此,组件类不需要标识符属性或标识符映射,他是一个简单的POJO.代码:
@Embeddable
public class Address {
@NotNull
@Column(nullable = false)
private String street;
@NotNull
@Column(nullable = false, length = 5)
private String zipcode;
@NotNull
@Column(nullable = false)
private String city;
public Address() {
}
public Address(String street, String zipcode, String city) {
this.street = street;
this.zipcode = zipcode;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
使用方法:
@Entity
@Table(name = "t_Users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//Address是@Embeddable 这里不需要注解
private Address homeAddress;
public Long getId() {
return id;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
}
Hibernate会检测Address类是否使用@Embeddable进行了注解, street, zipcode,city列是在t_Users表上映射的,即所属实体的表,
在更为现实的场景中,一个用户可能会将不同的地址用于不同的目的,例如User有家庭地址,还有账单地址:
重写嵌入属性
billingAddress是User类的另一个嵌入组件属性,所以必须在t_Users表中保存另一个address,这就造成了映射冲突,到目前为止,架构中只有在street,zipcode,city.中存储一个address列,需要额外的列来存储用于每个User行的另一个Address.映射billingAddress时,要重写列的名称
@Entity
@Table(name = "t_Users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Address homeAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street",
column = @Column(name = "billing_street")),
@AttributeOverride(name = "zipcode",
column = @Column(name = "billing_zipcode", length = 5)),
@AttributeOverride(name = "city",
column = @Column(name = "billing_city"))
})
private Address billingAddress;
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
public Long getId() {
return id;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
}
数据库实体:t_Users
@AttributeOverrides会选择地重写嵌入类的属性映射;在这个示例中,重写了全部3个属性并且提供了不同的列名,现在可以在t_Users表中存储两个Address实例了,每个实例位于不同的列中.
用于组件
用于组件属性的每个@AttributeOverride都完成了:重写的属性上的任何JPA或Hibernate都会忽略,这意味着Address类上的@Column注解会被忽略-----所有的billing_*列都可为null,(不过 Bean验证依旧会识别组件属性上的@NotNull注解;Hibernate只会重写持久化注解).
映射嵌套的可嵌入组件
考虑一下Address类以及它如何封装地址详情,不使用简单的city字符串.而是将这一个详情移动到一个新的City可嵌入类中,代码如下:
City类
@Embeddable
public class City {
@NotNull
@Column(nullable = false, length = 5)
private String zipcode;
@NotNull
@Column(nullable = false)
private String name;
@NotNull
@Column(nullable = false)
private String country;
public City() {
}
public City(String zipcode, String name, String country) {
this.zipcode = zipcode;
this.name = name;
this.country = country;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Address类
@Embeddable
public class Address {
@NotNull
@Column(nullable = false)
private String street;
@NotNull
@AttributeOverrides(
@AttributeOverride(name = "name",
column = @Column(name = "city", nullable = false))
)
private City city;
public Address() {
}
public Address(String street, City city) {
this.street = street;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
User类
@Entity
@Table(name = "t_Users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Address homeAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street",
column = @Column(name = "billing_street")),
@AttributeOverride(name = "city.zipcode",
column = @Column(name = "billing_zipcode", length = 5)),
@AttributeOverride(name = "city.name",
column = @Column(name = "billing_city")),
@AttributeOverride(name = "city.country",
column = @Column(name = "billing_country"))
})
private Address billingAddress;
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
public Long getId() {
return id;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
}
数据库实体:t_Users
可以在任何级别声明@AttributeOverrides,正如City类的name属性所做的那样,将它映射到City列,这可以使用(所示的)Address中的@AttributeOverride或者根实体类User中重写来实现,嵌套属性可以使用.符号来引用:例如,@AttributeOverride(name = "city.country",column = @Column(name = "billing_country"))引用了Address的#City#name属性.