public class Address { private String street; private String zipcode; private String city; public Address() { super(); } 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; } }User和Address之间的复合是一个元数据级的概念:你只需要告诉Hibernate,Address是映射文档中的值类型,或者是包含注解的值类型。
<component name="homeAddress" class="Address"> <property name="street" type="string" column="BILLING_STREET" not-null="true"/> <property name="zipcode" type="string" column="HOME_ZIPCODE" not-null="true"/> <property name="city" type="string" column="HOME_CITY" not-null="true"/> </component> <component name="billingAddress" class="Address"> <property name="street" type="string" column="BILLING_STREET" not-null="true"/> <property name="zipcode" type="string" column="BILLING_ZIPCODE" not-null="true"></property> <property name="city" type="string" column="BILLING_CITY" not-null="true"></property> </component>在<component>元素的内部声明Address的持久化属性。User类的属性被命名为homeAddress。
<component name="billingAddress" class="Address"> <parent name="user"/> <property name="street" type="string" column="BILLING_STREET" not-null="true"/> <property name="zipcode" type="string" column="BILLING_ZIPCODE" not-null="true"></property> <property name="city" type="string" column="BILLING_CITY" not-null="true"></property> </component><parent>元素把类型User的属性映射到自己的实体,在这个例子中,它是具名user的那个属性。然后可以调用Address.getUser()在另一个方向进行导航。这真是一个简单的后退指针。
<component name="homeAddress" class="Address"> <parent name="user"/> <component name="location" class="Location"> <property name="streetName" column="HOME_STREETNAME"/> <property name="streetSide" column="HOME_STREETSIDE"/> <property name="houseNumber" column="HOME_HOUSENUMBER"/> <property name="floor" column="HOME_FLOOR"/> </component> <property name="zipcode" type="string" column="HOME_ZIPCODE" not-null="true"/> <property name="city" type="string" column="HOME_CITY" not-null="true"/> </component>Location类的设计相当于Address类。你现在有3个类、1个实体和2个值类型,全部被映射到同一张表。
@Entity @Table(name = "TBL_USER") public class User{ ... @Embedded private Address homeAddress; ... }如果没有把一个属性声明为@Embedded,并且它不是JDK类型,Hibernate就在被关联的类中查找@Embedded注解。如果它存在,属性就自动地被映射为一个依赖的组件。
@Embeddable public class Address { @Column(name = "ADDRESS_STREET", nullable = false) private String street; @Column(name = "ADDRESS_ZIPCODE", nullable = false) private String zipcode; @Column(name = "ADDRESS_CITY", nullable = false) private String city; public Address() { super(); } public Address(String street, String zipcode, String city) { super(); 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 = "TBL_USER") public class User{ ... @Embedded @AttributeOverrides( { @AttributeOverride( name = "street", column = @Column(name = "HOME_STREET")), @AttributeOverride( name = "zipcode", column = @Column(name = "HOME_ZIPCODE")), @AttributeOverride( name = "city", column = @Column(name = "HOME_CITY")) }) private Address homeAddress; ... }User类中的这个新的@Column声明覆盖了被嵌入的类的设置。注意,被嵌入的@Column注解上的所有属性都被替换,因此它不同nullabe = false。
<entity class="auctin.model.User" access="PROPERTY"> <attributes> <embedded name="homeAddress"> <attribute-override name="street"> <column name="HOME_STREET"/> </attribute-override> <attribute-override name="zipcode"> <column name="HOME_ZIPCODE"/> </attribute-override> <attribute-override name="city"> <column name="HOME_CITY"/> </attribute-override> </embedded> </attributes> </entity>对于类被映射为组件有2条重要的限制。
Hibernate实战_笔记26(映射组件),布布扣,bubuko.com
原文:http://blog.csdn.net/com185272358/article/details/21391025