2008-05-20
Lightweight Class In Hibernate3
Lightweight Class In Hibernate3
Suppose I have the following persistent class:
假如有这样一个持久化类:
这个类中的所有属性都与数据库中的DOCUMENTS表中的某一列对应。
但是实例化这个类中的_text字段要消耗很大的内存。所以如果我要对这个表进行操作比如列出所有document的名字,或者给某个docuemnt改名。那么我又不想load出Clob类型的_text这个属性。那么怎么做呢?当然,有很多方法,而下面的方法是Hibernate官方网站
推荐的方法:
我们可以把这个持久化类分为"lightweight" superclass和 "heavyweight" subclass
如下:
We use the following mapping:
ok,如果我们要得到一个不包含_text属性的持久化对象,可以这样:
只要这样(注意hibernate2不支持)
如果你希望同时查询出同一个id的DocumentInfo 和 Document两个对象,你需要在load DocumentInfo 之后
使用session.evict()或者在mapping文件中设置polymorphism="explicit"
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"
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 3730 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Ext核心代码分析之Functi ...
myclass.alert=window.alert.createDelegat ...
-- by flare -
Ext核心代码分析之Functi ...
magicyang918 写道我在单纯的HTML上用这个方法会报错啊, 代码如 ...
-- by chenjf2k -
Ext核心代码分析之Functi ...
我在单纯的HTML上用这个方法会报错啊, 代码如下: <script> ...
-- by magicyang918 -
Ext核心代码分析之Functi ...
createDelegate更大的用处是在于指定Function执行的作用域 简 ...
-- by 南宫小骏 -
Ext与JavaEE之我见(第一 ...
fangzhouxing 写道引用是否可以抛弃使用web前端框架比如struts ...
-- by wangxin0072000






评论排行榜