Lightweight Class In Hibernate3

[b]Lightweight Class In Hibernate3[/b]


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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值