- 浏览: 98459 次
- 性别:
- 来自: 北京
文章分类
最新评论
1、概念
1.1 聚集关系
在域模型中,有些类由几个部分类组成,部分类的对象的生命周期依赖于整体类的对象的生命周期,当整体消失时,部分也就随之消失,这种整体与部分的关系被称为聚集关系。
1.2 组成关系
如果部分只能属于特定的整体,这种聚集关系也叫做组成关系。
如:任何手足事整体与部分的关系,每双手只能属于特定的人。
2、粒度
建立关系数据模型的一个重要原则:在不会导致数据冗余的前提下,尽可能减少数据库表的数目以及表之间的外键参照关系,这样可以避免建立多表连接。
3. 映射组成关系
建立域模型和关系数据模型有着不同的出发点。域模型是由程序代码组成的,通过细化持久化类的粒度可提高代码可重用性,简化编程。关系数据模型是由关系数据组成的。在存在数据冗余的情况下,需要把粗粒度的表拆分成具有外键参照关系的几个细粒度的表,从而节省存储空间;另一方面,在没有冗余数据的前提下,应该尽可能减少表的数目,简化表之间的参照关系,以便提高数据库的访问速度。
例:
映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
<class name="mypack.Customer" table="CUSTOMERS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="name" type="string" >
<column name="NAME" length="15" />
</property>
<component name="homeAddress" class="mypack.Address">
<parent name="customer" />
<property name="province" type="string" column="HOME_PROVINCE"/>
<property name="city" type="string" column="HOME_CITY"/>
<property name="street" type="string" column="HOME_STREET"/>
<property name="zipcode" type="string" column="HOME_ZIPCODE"/>
</component>
<component name="comAddress" class="mypack.Address">
<parent name="customer" />
<property name="province" type="string" column="COM_PROVINCE"/>
<property name="city" type="string" column="COM_CITY"/>
<property name="street" type="string" column="COM_STREET"/>
<property name="zipcode" type="string" column="COM_ZIPCODE"/>
</component>
</class>
</hibernate-mapping>
<component>元素表明homeAddress属性是Customer类的一个组成部分,在hibernate中称之为组件.
<parent>子元素指定Address类所属的整体类,应定义为Address中的一个属性,同时对应的getter、setter方法。
<property>子元素用来配置组件类的属性和表的字段的映射。
3.1 区分值(Value)类型和实体(Entity)类型
上面Address类的定义如下:
package mypack;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Address implements Serializable {
/** nullable persistent field */
private String province;
/** nullable persistent field */
private String city;
/** nullable persistent field */
private String street;
/** nullable persistent field */
private String zipcode;
/** nullable persistent field */
private mypack.Customer customer;
/** full constructor */
public Address(String province, String city, String street, String zipcode, mypack.Customer customer) {
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
this.customer = customer;
}
/** default constructor */
public Address() {
}
public String getProvince() {
return this.province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public mypack.Customer getCustomer() {
return this.customer;
}
public void setCustomer(mypack.Customer customer) {
this.customer = customer;
}
public String toString() {
return new ToStringBuilder(this)
.toString();
}
}
可以发现Address类没有OID,这是Hibernate组件的一个重要特征,没有OID也就不能通过Session来单独管理,否则会抛出MappingException。
持久化类分为两种,值类型和实体类型,最重要的区别是前者没有OID,不能被单独持久化,他的生命周期依赖于所属的持久化类对象的生命周期,组件类型就是一种值类型。
4、映射复合组成关系
配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
<class name="mypack.Computer" table="COMPUTERS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="type" type="string" >
<column name="COMPUTER_TYPE" length="15" />
</property>
<component name="cpuBox" class="mypack.CpuBox">
<parent name="computer" />
<property name="type" type="string" >
<column name="CPUBOX_TYPE" length="15" />
</property>
<component name="graphicsCard" class="mypack.GraphicsCard">
<parent name="cpuBox" />
<property name="type" type="string" >
<column name="GRAPHICSCARD_TYPE" length="15" />
</property>
</component>
<many-to-one
name="vendor"
column="CPUBOX_VENDOR_ID"
class="mypack.Vendor"
not-null="true"
/>
</component>
</class>
</hibernate-mapping>
定义:
package mypack;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Computer implements Serializable {
/** identifier field */
private Long id;
/** nullable persistent field */
private String type;
/** persistent field */
private mypack.CpuBox cpuBox;
/** full constructor */
public Computer(String type, mypack.CpuBox cpuBox) {
this.type = type;
this.cpuBox = cpuBox;
}
/** default constructor */
public Computer() {
}
/** minimal constructor */
public Computer(mypack.CpuBox cpuBox) {
this.cpuBox = cpuBox;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public mypack.CpuBox getCpuBox() {
return this.cpuBox;
}
public void setCpuBox(mypack.CpuBox cpuBox) {
this.cpuBox = cpuBox;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
package mypack;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class CpuBox implements Serializable {
/** nullable persistent field */
private String type;
/** nullable persistent field */
private mypack.Computer computer;
/** persistent field */
private mypack.Vendor vendor;
/** persistent field */
private mypack.GraphicsCard graphicsCard;
/** full constructor */
public CpuBox(String type, mypack.Computer computer, mypack.Vendor vendor, mypack.GraphicsCard graphicsCard) {
this.type = type;
this.computer = computer;
this.vendor = vendor;
this.graphicsCard = graphicsCard;
}
/** default constructor */
public CpuBox() {
}
/** minimal constructor */
public CpuBox(mypack.Vendor vendor, mypack.GraphicsCard graphicsCard) {
this.vendor = vendor;
this.graphicsCard = graphicsCard;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public mypack.Computer getComputer() {
return this.computer;
}
public void setComputer(mypack.Computer computer) {
this.computer = computer;
}
public mypack.Vendor getVendor() {
return this.vendor;
}
public void setVendor(mypack.Vendor vendor) {
this.vendor = vendor;
}
public mypack.GraphicsCard getGraphicsCard() {
return this.graphicsCard;
}
public void setGraphicsCard(mypack.GraphicsCard graphicsCard) {
this.graphicsCard = graphicsCard;
}
public String toString() {
return new ToStringBuilder(this)
.toString();
}
}
发表评论
-
Hibernate入门
2010-10-27 00:11 713Java代码 1.public class User { ... -
hibernate二级缓存攻略
2010-10-20 15:31 570很多人对二级缓存都不太了解,或者是有错误的认识,我一直想写一篇 ... -
精通hibernate学习笔记(8-2)[检索方式]
2010-10-19 13:01 7094、报表查询 报表查询用于对数据分组和统计,完整的HQL语 ... -
精通hibernate学习笔记(8)[检索方式]
2010-10-19 13:00 754提供的检索方式: (1)导航对象图检索方式 (2)OID ... -
精通hibernate学习笔记(7)[检索策略]
2010-10-19 12:55 766Customer和Order为例 1、hibernate检索 ... -
精通hibernate学习笔记(6)[映射类型]
2010-10-19 12:51 741Hibernate映射类型分为两种:内置映射类型和客户化映射类 ... -
精通hibernate学习笔记(4)[操作持久化对象]
2010-10-19 12:47 6801、理解Session的缓存 如果希望一个java对象一直处 ... -
精通hibernate学习笔记(3)[关联关系]
2010-10-19 12:43 903关联关系分:单向关联 ... -
精通hibernate学习笔记(2)[标志符生成器]
2010-10-19 12:37 5611、increment 标识符生成器 该生成器由Hiber ... -
精通hibernate学习笔记(1)
2010-10-19 12:36 743实体域对象的 持久化模式 1、ORM模式 对象-关 ... -
分析Hibernate的缓存机制
2010-10-19 12:33 608缓存是介于应用程序和 ... -
Hibernate缓存管理
2010-10-19 12:31 5841. Cache简介: 缓存( ... -
Hibernate 学习笔记
2010-10-19 12:28 782原创 hibernate 笔记 收藏 day1 一. h ...
相关推荐
hibernate学习笔记 hibernate学习笔记 hibernate学习笔记 hibernate学习笔记 hibernate学习笔记 hibernate学习笔记 hibernate学习笔记 hibernate学习笔记
Hibernate学习笔记_特别详细 Hibernate学习笔记_特别详细
Hibernate学习笔记 Hibernate学习笔记 Hibernate学习笔记 Hibernate学习笔记
hibernate 对象关系映射总结hibernate 对象关系映射总结hibernate 对象关系映射总结
学习hibernate框架时,整理的笔记,对hibernate框架有全面的介绍
hibernate配置关系的笔记
Hibernate学习笔记Hibernate学习笔记Hibernate学习笔记Hibernate学习笔记
本人在厦门邦初培训时的快速入门精简文档 建立具有组成关系的域模型 建立粗粒度的关系模型 映射组成关系 区分值类型和实体类型
Hibernate映射解析 七种映射关系
精通Hibernate读书笔记
hibernate 学习笔记精要hibernate 学习笔记精要hibernate 学习笔记精要hibernate 学习笔记精要
达内HIBERNATE学习笔记,达内HIBERNATE学习笔记,达内HIBERNATE学习笔记,达内HIBERNATE学习笔记,
《精通Hibernate》学习-对象关系映射基础.zip
Java相关课程系列笔记之十四Hibernate学习笔记
这套笔记是我学习Hibernate,进行相关技术点训练时记录下来的,其中包括技术点说明与相关事例,拿出来与大家分享。
hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...
Hibernate4中映射关系图解。
hibernate学习笔记 hibernate
Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者