浏览 190 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-05-20
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" 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |


