應用場景:
從對象建模的角度來看,如果兩個對象之間是一種聚集——是整體的一部分(part-of)的關係,聚集是一種強健的關聯形式:它對於對象的生命週期有一些額外的語義。在這種情況下,我們有一種更強健的形式:複合(composition),在這裏部分的生命週期完全依賴於整體的生命週期。對象建模專家和UML設計師聲稱,就實際的java實現而言,在這個複合和其他更弱形式的關聯之間是沒有區分的。但是在ORM的上下文種,則有一個很大德區別:複合的關係經常是一個備選的值類型。
示例實體:
User Address一個 User有一個家庭地址(homeAddress)和一個賬單地址(billingAddress)。User是實體,而Address是值類型。他們是聚集關係。
一、XML的實現
package com.ccay.test.componentMapping_xml;
public class User {
private long userId;
private String firstname;
private String lastname;
private Address homeAddress;
private Address billingAddress;
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
package com.ccay.test.componentMapping_xml;
public class Address {
private User user;
private String street;
private String zipcode
private String city;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
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;
}
}
package com.ccay.test.componentMapping_xml;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.ccay.test.HibernateSessionFactory;
public class Test {
@org.junit.Test
public void testDDL(){
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
Address homeAddress = new Address();
homeAddress.setCity("china");
homeAddress.setStreet("zhongshan");
homeAddress.setZipcode("010");
Address billingAddress = new Address();
billingAddress.setCity("china");
billingAddress.setStreet("zhongshan2");
billingAddress.setZipcode("0102");
User user= new User();
user.setFirstname("firstName");
user.setLastname("lastName");
user.setHomeAddress(homeAddress);
user.setBillingAddress(billingAddress);
session.save(user);
transaction.commit();
HibernateSessionFactory.closeSession();
}
}
HBM.XML映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ccay.test.componentMapping_xml.User" table="COM_MAP_USER" >
<id name="userId" type="long">
<column name="USER_ID"/>
<generator class="native" />
</id>
<property name="firstname" type="string">
<column name="FIRSTNAME" length="40" />
</property>
<property name="lastname" type="string">
<column name="LASTNAME" length="50" />
</property>
<component name="homeAddress" class="com.ccay.test.componentMapping_xml.Address">
<!-- 可加入後退指針 -->
<parent name="user"/>
<property name="street" type="string" column="HOME_STREET" not-null="true"/>
<property name="city" type="string" column="HOME_CITY" not-null="true"/>
<property name="zipcode" type="string" column="HOME_ZIPCODE" not-null="true"/>
</component>
<component name="billingAddress" class="com.ccay.test.componentMapping_xml.Address">
<parent name="user"/>
<property name="street" type="string" column="BILLING_STREET" not-null="true"/>
<property name="city" type="string" column="BILLING_CITY" not-null="true"/>
<property name="zipcode" type="string" column="BILLING_ZIPCODE" not-null="true"/>
</component>
</class>
</hibernate-mapping>
hibernate自動 DDL
create table COM_MAP_USER (
USER_ID bigint not null auto_increment,
FIRSTNAME varchar(40),
LASTNAME varchar(50),
HOME_STREET varchar(255) not null,
HOME_CITY varchar(255) not null,
HOME_ZIPCODE varchar(255) not null,
BILLING_STREET varchar(255) not null,
BILLING_CITY varchar(255) not null,
BILLING_ZIPCODE varchar(255) not null,
primary key (USER_ID)
)
powerDesigner 逆向工程如圖
二、註解的實現
package com.ccay.test.componentMapping_JAPAnnotation;
import javax.annotation.Generated;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "COM_MAP_USER")
public class User {
@Id
@GeneratedValue
private long userId;
private String firstname;
private String lastname;
@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;
@Embedded
private Address billingAddress;
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
package com.ccay.test.componentMapping_JAPAnnotation;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.annotations.Parent;
@Embeddable
public class Address {
@Parent//JPA中沒有 所以用 org.hibernate.annotations.Parent
private User user;
@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 User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
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;
}
}
hibernate 自動DDL
create table COM_MAP_USER (
userId bigint not null auto_increment,
ADDRESS_CITY varchar(255) not null,
ADDRESS_STREET varchar(255) not null,
ADDRESS_ZIPCODE varchar(255) not null,
firstname varchar(255),
HOME_CITY varchar(255),
HOME_STREET varchar(255),
HOME_ZIPCODE varchar(255),
lastname varchar(255),
primary key (userId)
)
逆向工程