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"
分享到:
相关推荐
介绍了RFID系统、可用的轻量级密码技术、各种可能的攻击方式,以及EPC class-1 generation-2
Beginning Hibernate is ideal if you’re experienced in Java with databases (the traditional, or connected, approach), but new to open-source, lightweight Hibernate. The book keeps its focus on ...
Beginning Hibernate is ideal if you’re experienced in Java with databases (the traditional, or connected, approach), but new to open-source, lightweight Hibernate. The book keeps its focus on ...
http://www.sics.se/~adam/pt/download.html
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...
How to work with Spring JDBC, use declarative transactions with Spring, and reap the benefits of a lightweight persistence strategy How to work with Hibernate and integrate it into your Spring-based ...
A lightweight 3D Morphable Face Model library in modern C++
lightweight清爽简洁的编辑器 lightweight清爽简洁的编辑器
Securing Internet of Things with Lightweight IPsec Securing Internet of Things with Lightweight IPsec
It also provides a comprehensive review of the design process in the electric hybrid drive and energy storage systems.Ideal for automotive engineering students and professionals Lightweight Electric/...
LightWeight IP Application Examples开发例程,英文原版,给需要lwip移植的开发者。
Latest version of "Lightweight Django" Suitable if you've finished the official tutorial and re start Django research
NanoScript - A Lightweight Scripting Engine for .NET
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...
Lightweight_Django.pdf
ALIKE: Accurate and Lightweight Keypoint Detection and Descripto
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 ...
Lightweight Stream Control Protocol轻量级流控制协议