实体粒度划分顾名思义就是将一个比较大的对象进行拆分,划分为几个小的对象。但是这种粒度的划分只是针对类对象,而不是针对数据表。
实体的细粒度划分优点:
1,通过细粒度划分使系统逻辑更加清晰
2,性能方面,能提高系统的能耗比(性能/资源消耗)
举例如下:
1,新建表person
create table Person(
id number(20) primary key,
firstName varchar2(50) not null,
lastName varchar2(50) not null,
telphone varchar2(11) ,
email varchar2(30),
zipcode varchar2(30)
)
2,因为类的属性太多,故可以将person类进行粒度的划分,将属性进行归类。
public class Name {
private String firstName = "";
private String lastName = "";
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 class Contact {
private String email = "";
private String telphone = "";
private String zipcode = "";
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
将以上两个类作为Person类的属性
public class Person {
private int id ;
private Name name ;
private Contact contact;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
3,Person.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 package="包名"> <class name="Person" table="Person"> <id name="id" column="ID" type="int"> <generator class="sequence"> <param name="sequence">序列名</param> </generator> </id> <component name="name" class="Name"> <property name="firstName" column="firstName" type="string"/> <property name="lastName" column="lastName" type="string"/> </component> <component name="contact" class="Contact"> <property name="email" column="email" type="string"/> <property name="telphone" column="telphone" type="string"/> <property name="zipcode" column="zipcode" type="string"/> </component> </class> </hibernate-mapping>