Hibernate3学习笔记(二) —— 实体粒度设计

实体粒度设计

Fine-grained Object Model    “设当的细粒度模型”。

“细粒度”,将原本业务模型中的对象加以细分,得到更加精细的对象模型。

两个目的:

    面向设计的粒度细分:通过对象细化,实现更加清晰的系统逻辑;

    面向性能的粒度细分:针对业务逻辑,通过合理的细粒度对象,提高系统的能耗比(性能/资源消耗)。

1、面向设计的细粒度细分

    在Hibernate中可借助于Component(组件)节点的定义完成。Hibernate语义:将某个实体对象只能够的一个逻辑组成称为Component。

    Component没有标识(Identity),作为一个逻辑组成,完全从属于实体对象。示例片段:

pubblic class Tperson{

    private Integer id;

    private Contact contact;

    private Name name;

    //gettter/setter

}

public class Contact{

    private String address;

    //gettter/setter

}

public class Name{

    private String name;

    //gettter/setter

}

在Hibernate中通过component节点对组件声明

<hibernate-mapping>

    <class name="Tperson" table="t_person">

        <id name="id" column="id">

            <generator class="native" />

        </id>

        <component name="name" class="Name">

            <property name="name" type="String" column="name"/>

        </component>

        <component name="contact" class="Contact">

            <property name="address" type="String" column="name"/>

        </component>

    </class>

</hibernate-mapping>

注:通过Component定义门将t_person表映射成了3个类,TPErson、Contact和Name,在传统关系型库表上,实现了面向对象的领域划分。

2、面向性能的粒度细分

若t_person中存在“重量级”字段类型,如blob等,数据库读取操作的代价较高,在Hibernate3中提供了一种属性延迟加载功能。但也可采用另外一种策略(Hibernate2)

将TPerson对象拆分为TPersonInfo和TPersonProfile

public class TPersonInfo{

    private ......

    //getter/setter

}

private class TPersonProfile extends TPersonInfo {

    private Blob image;

    //getter/setter

}

通过继承关系进行纵向细分

TPersonInfo.hbm.xml片段:

<hibernate-mapping>

    <class name="TPersonInfo" table="t_person">

        <id ......></id>

        <property .../>

    </class>

</hibernate-mapping>

TPersonProfile .hbm.sml片段:

<hibernate-mapping>

    <class name="TPersonProfile " table="t_person" polymorphism="explicit">

        <id ......></id>

        <property .../>

    </class>

</hibernate-mapping>

注意:通过polymorphism="explicit"声明了一个显示多态关系,即只有在明确指定类名的时候才会返沪此类实例,

如list=session.createQuery("from TPersonProfile ").list();返回的是TPersonProfile 实例。而list=session.createQuery("from Objects").list();返回的是TPersonProfile 的父类TPersonInfo实例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值