Hibernate提倡在建模时,进行细粒度的控制。这个抽象的概念可以这样理解,一般在进行Hibernate持久化对象建模时,一个对象对应一个配置文件,在配置文件中的属性都有这样一个对象中的属性对应。试想如果一个对象的属性有十几二十个,那么这个对象显然就会变得很大,尤其是getter和setter方法。这样显然会增加对象的复杂度,使得对象变得难以理解。那这时该怎么办呢。Hibernate为我们提供了这样一个途径。那就是利用Component。即这么多属性中必然有些属性是在概念上相近的,如一个Person对象中有这样三个属性,firstname,middlename,lastname,这三个属性都和名字有关,所以可以提成一个Name对象作为Person的Component。这就是Hibernate提倡的细粒度控制了。这样对象就变得易于理解,更为接近现实中的情况,使项目变得易于扩展和维护。那代码上具体如何实现呢。
1.FullName.java
package com.template.model.person;
/**
* Created by IntelliJ IDEA.
* User: ZhongGang
* Date: 11-7-21
* Time: 下午8:22
* To change this template use File | Settings | File Templates.
*/
public class FullName {
private String firstName;
private String middleName;
private String lastName;
public FullName() {
}
}
2.Name.java
package com.template.model.person;
/**
* Created by IntelliJ IDEA.
* User:ZhongGang
* Date: 11-7-21
* Time: 下午8:22
* To change this template use File | Settings | File Templates.
*/
public class Name {
private FullName fullName;
private String nickName;
public Name() {
}
}
3.Address.java
package com.template.model.person;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-7-21
* Time: 下午8:24
* To change this template use File | Settings | File Templates.
*/
public class Address {
private String street;
private String city;
private String country;
public Address() {
}
}
4.Human.java
package com.template.model.person;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-7-21
* Time: 下午8:21
* To change this template use File | Settings | File Templates.
*/
public class Human extends BasicDomain {
private Name name;
private Address address;
public Human() {
}
}
5.Human.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.template.model.person.Human" table="human"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="active" column="active" type="java.lang.Boolean"/> <component name="name" class="com.template.model.person.Name"> <component name="fullName" class="com.template.model.person.FullName"> <property name="firstName" column="firstname" type="java.lang.String"/> <property name="middleName" column="middlename" type="java.lang.String"/> <property name="lastName" column="lastname" type="java.lang.String"/> </component> <property name="nickName" column="nickname" type="java.lang.String"/> </component> <component name="address" class="com.template.model.person.Address"> <property name="street" column="street" type="java.lang.String"/> <property name="country" column="country" type="java.lang.String"/> <property name="city" column="city" type="java.lang.String"/> </component> </class> </hibernate-mapping>
从Name和FullName可以看出,Hibernate支持多层的Component嵌套。
如果想实现从Name知道是哪个Human,应该怎么办呢?看下面的代码。只须作稍许的修改。
Name.java
package com.template.model.person;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-7-21
* Time: 下午8:22
* To change this template use File | Settings | File Templates.
*/
public class Name {
private FullName fullName;
private String nickName;
private Human human;
public Name() {
}
public Human getHuman() {
return human;
}
public void setHuman(Human human) {
this.human = human;
}
}
Human.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.template.model.person.Human" table="human"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="active" column="active" type="java.lang.Boolean"/> <component name="name" class="com.template.model.person.Name"> <parent name="human"/> <component name="fullName" class="com.template.model.person.FullName"> <property name="firstName" column="firstname" type="java.lang.String"/> <property name="middleName" column="middlename" type="java.lang.String"/> <property name="lastName" column="lastname" type="java.lang.String"/> </component> <property name="nickName" column="nickname" type="java.lang.String"/> </component> <component name="address" class="com.template.model.person.Address"> <property name="street" column="street" type="java.lang.String"/> <property name="country" column="country" type="java.lang.String"/> <property name="city" column="city" type="java.lang.String"/> </component> </class> </hibernate-mapping>