`
wangxin0072000
  • 浏览: 87374 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lightweight Class In Hibernate3

    博客分类:
  • java
阅读更多
Lightweight Class In Hibernate3


Suppose I have the following persistent class:
假如有这样一个持久化类:
public class Document implements Node {
   private Long _key;
   private String _name;
   private Calendar _created;
   private Calendar _updated;
   private Folder _folder;
   private Clob _text;
   public String getKey() { return _key; }
   public void setKey(Long key) { _key = key; }
   public String getName() { return _name; }
   public void setName(String name) { _name = name; }
   public Calendar getCreated() { return _created; }
   public void setCreated(Calendar created) { _created = created; }
   public Calendar getUpdated() { return _updated; }
   public void setUpdated(Calendar updated) { _updated = updated; }
   public Folder getFolder() { return _folder; }
   public void setFolder(Folder folder) { _folder = folder; }
   public Clob getText() { return _text; }
   public void setText(Clob text) { _text = text; }
}

这个类中的所有属性都与数据库中的DOCUMENTS表中的某一列对应。
但是实例化这个类中的_text字段要消耗很大的内存。所以如果我要对这个表进行操作比如列出所有document的名字,或者给某个docuemnt改名。那么我又不想load出Clob类型的_text这个属性。那么怎么做呢?当然,有很多方法,而下面的方法是Hibernate官方网站
推荐的方法:
我们可以把这个持久化类分为"lightweight" superclass和 "heavyweight" subclass
如下:

public class DocumentInfo implements Node {
   private Long _key;
   private String _name;
   private Calendar _created;
   private Calendar _updated;
   private Folder _folder;
   private Clob _text;
   public String getKey() { return _key; }
   public void setKey(Long key) { _key = key; }
   public String getName() { return _name; }
   public void setName(String name) { _name = name; }
   public Calendar getCreated() { return _created; }
   public void setCreated(Calendar created) { _created = created; }
   public Calendar getUpdated() { return _updated; }
   public void setUpdated(Calendar updated) { _updated = updated; }
   public Folder getFolder() { return _folder; }
   public void setFolder(Folder folder) { _folder = folder; }
}
public class Document extends DocumentInfo {
   private Clob _text;
   public Clob getText() { return _text; }
   public void setText(Clob text) { _text = text; }
}

We use the following mapping:

<class name="DocumentInfo" table="DOCUMENTS">
   <id name="key" type="long" column="ID">
       <generator class="native"/>
   </id>
   <property name="name"/>
   <property name="created"/>
   <property name="updated"/>
   <many-to-one name="folder"/>
</class>

<class name="Document" table="DOCUMENTS" polymorphism="explicit">
   <id name="key" type="long" column="ID">
       <generator class="native"/>
   </id>
   <property name="name"/>
   <property name="created"/>
   <property name="updated"/>
   <many-to-one name="folder"/>
   <property name="text"/>
</class>

ok,如果我们要得到一个不包含_text属性的持久化对象,可以这样:
from DocumentInfo
from Node
from java.lang.Object
同样由于我们在mapping 文件中设置了polymorphism="explicit",所以如果我们希望得到包含_text属性的持久化对象
只要这样(注意hibernate2不支持)
from d in class Document
DocumentInfo info = (DocumentInfo) session.load(DocumentInfo.class, new Long(1));
Document doc = (Document) session.load(Document.class, new Long(1));

如果你希望同时查询出同一个id的DocumentInfo 和 Document两个对象,你需要在load DocumentInfo 之后
使用session.evict()或者在mapping文件中设置polymorphism="explicit"
分享到:
评论

相关推荐

    Lightweight Cryptography in Radio Frequency Identification (RFID) Systems

    EPC Class-1 Generation-2(也称为Gen2)是由电子产品代码全球组织(EPCglobal)制定的一项RFID技术标准。该标准旨在提高RFID标签在供应链管理中的性能和互操作性。EPC Gen2规定了RFID标签、阅读器及通信协议的具体...

    Protothreads - Lightweight, Stackless Threads in C

    Protothreads是一种轻量级、无栈线程的概念,主要在嵌入式系统和资源有限的环境中使用,由Adam Dunkels提出。它不是操作系统内核的一部分,而是在用户空间实现的一种并发编程模型。Protothreads的目的是解决微控制器...

    Beginning Hibernate, 3rd Edition

    Beginning Hibernate, Third Edition is ideal if you’re experienced in Java with databases (the traditional, or «connected,» approach), but new to open-source, lightweight Hibernate, a leading object...

    lightweight清爽简洁的编辑器

    《lightweight清爽简洁的编辑器探索》 在数字化时代,编辑器成为了我们日常工作中不可或缺的工具,特别是对于程序员、作家以及任何需要处理文本的人来说。"lightweight清爽简洁的编辑器"这一主题,恰恰迎合了现代...

    Lightweight Django

    Latest version of "Lightweight Django" Suitable if you've finished the official tutorial and re start Django research

    MIBS: A New Lightweight Block Cipher

    In this paper, we propose a new lightweight 64-bit block cipher, which we call MIBS, suitable for resource-constrained devices, such as low-cost RFID tags. We also study its hardware implementation ef...

    ldap提示object class violation

    在LDAP(Lightweight Directory Access Protocol,轻量目录访问协议)的使用过程中,有时会遇到一个常见的错误提示:“object class violation”。这一错误提示通常出现在尝试对LDAP目录进行修改操作时,如添加、...

    lightweight-search.zip

    3. **lightweight-search.jar**:这是一个Java可执行文件,表明该搜索工具可能是用Java语言开发的。Java的跨平台特性使得该软件能在多种操作系统上运行。.jar文件是Java的归档文件,包含了类文件和其他资源,可以...

    LightWeight IP Application Examples

    此外,开发环境需要配置Xilinx Vivado 2014.3来创建硬件修改,以及Xilinx SDK 2014.3来运行或创建对软件的硬件修改。这包括对AC701、KC705和ZC-702的硬件修改。 在软件开发方面,lwIP库能够提供给开发者一系列的...

    Lightweight J2EE Architecture

    Lightweight J2EE Architecture 轻量级J2EE架构

    MQTT Essentials - A Lightweight IoT Protocol

    MQTT Essentials - A Lightweight IoT Protocol by Gaston C. Hillar English | 14 Apr. 2017 | ASIN: B01MS9I105 | 280 Pages | AZW3 | 6.19 MB Key Features Make your connected devices less prone to ...

    Securing Internet of Things with Lightweight IPsec

    ### 使用轻量级IPsec保障物联网安全 #### 引言 随着无线传感器网络(Wireless Sensor Networks, WSNs)的广泛应用以及与传统IP网络的紧密集成,物联网(Internet of Things, IoT)的安全问题变得日益突出。...

    Lightweight Django epub

    Lightweight Django 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Python库 | lightweight-gan-0.14.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:lightweight-gan-0.14.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    中文版Lightweight-Mesh-Developer-Guide_Application

    ### Lightweight Mesh 开发指南知识点详解 #### 一、概述 **Lightweight Mesh** 是一款专为物联网(IoT)设计的轻量级、易于使用的无线网状网络协议栈,适用于那些需要广泛无线连接的应用场景。它由爱特梅尔公司...

    A lightweight 3D Morphable Face Model library in modern C++.zip

    3. **面部特征匹配**:为了将3DMM应用于实际人脸,库可能提供了面部特征检测和对齐的功能,比如关键点检测,以确保模型与真实人脸的准确对应。 4. **模型拟合**:通过最小化误差函数,库可以调整形状和纹理向量,使...

Global site tag (gtag.js) - Google Analytics