如果多个类中有相同的属性字段,那么把相同的属性字段提取出公共的连接类。
该连接类为值类而不是实体类。
在hibernate中,component是实体对象的逻辑组成部分,component与实体对象的本质区别是它没有oid采用component的好处,它实现了对象层次的细粒度划分,层次分明,利于复用。
用户类:
package com.jzq.hibernate;
public class User {
private int id;
private String email;
private Contect contect;
public User(){}
public Contect getContect() {
return contect;
}
public void setContect(Contect contect) {
this.contect = contect;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
公共的组件类:
package com.jzq.hibernate;
public class Contect {
private String zipCode;
private String address;
public Contect(){}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
组件配置文件:
<hibernate-mapping>
<class name="Animal" package="com.jzq.hibernate">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<!-- 把类中公共属性当做组件引入 -->
<component name="contact">
<property name="contactTel"/>
<property name="email"/>
<property name="zipCode"/>
<property name="address"/>
</component>
</class>
</hibernate-mapping>