Huion
事业向左...爱情向右...失去的...我会在未来期待...
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 1,comments - 3,trackbacks - 0
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔档案
2005年6月 (1)
文章分类
Hibernate(1)
J2SE(1)
My Design(1)
问题解决列表(1)
文章档案
2006年3月 (1)
2005年7月 (1)
2005年6月 (2)
搜索
最新评论
1. re: J2SE基础知识之不要依懒Object.equals();
这个是菜鸟都懂啦,哈哈哈哈
--weibo
2. re: 我的设计之一:利用Java映射和Jdom做通用JavaBean取存
hao
--weibo
3. re: 我追求...我快乐...
这样的文章应该放在非技术区!
--dudu
Light Weight 模式的实现
问题提出:
最近项目出现一个Article持久类,该持久类拥有一个Clob属性用于记录文章的内容!示例如下:
public
class
Article implements Serializable
{
public
Long articleId;
public
String author;
public
String title;
public
Date created;
public
Clob content;
//
getter
//
setter
}
后来发现,在查找文章列表的时候,每个Article都要从数据库中加载出content!严重影响效率!
解决方案1:
在网上找相关的资料,利用hql可以解决问题:
select
new Article(a.articleId,a.author,a.title,a.created)
from
Article
as
a
where
记得要为 Article 添加构造函数,像上面的hql需要构造函数如:
public
Article(Long articleId,String author,String title,Date create)
{
this
.articleId
=
articleId;
this
.author
=
author;
this
.title
=
title;
this
.create
=
create;
}
解决方案2:
然而这样子做,列表里面的Article还不够轻量,于是突然有天在Hibernate官方网站找出更好的解决方法!那就是所谓的Light Weight模式,示例代码如下:
public
class
ArticleInfo implements Serializable
{
public
Long articleId;
public
String author;
public
String title;
public
Date created;
//
getter
//
setter
}
public
class
Article extends ArticleInfo
{
public
Clob content;
//
getter
//
setter
}
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="test.ArticleInfo"
table
="test_article"
polymorphism
="explicit"
>
<
id
name
="articleId"
column
="article_id"
type
="java.lang.Long"
length
="30"
>
<
generator
class
="sequence"
>
<
param
name
="sequence"
>
article_seq
</
param
>
</
generator
>
</
id
>
<
property
name
="authorId"
column
="author_id"
type
="java.lang.String"
length
="10"
not-null
="true"
/>
<
property
name
="title"
column
="title"
type
="java.lang.String"
length
="10"
not-null
="true"
/>
<
property
name
="created"
column
="created"
type
="java.util.Date"
not-null
="true"
/>
</
class
>
</
hibernate-mapping
>
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<!
DOCTYPE hibernate
-
mapping PUBLIC
"
-//Hibernate/Hibernate Mapping DTD 2.0//EN
"
"
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
"
>
<
hibernate
-
mapping
>
<
class
name
=
"
test.Article
"
table
=
"
test_article
"
polymorphism
=
"
explicit
"
>
<
id name
=
"
articleId
"
column
=
"
article_id
"
type
=
"
java.lang.Long
"
length
=
"
30
"
>
<
generator
class
=
"
sequence
"
>
<
param name
=
"
sequence
"
>
article_seq
</
param
>
</
generator
>
</
id
>
<
property name
=
"
authorId
"
column
=
"
author_id
"
type
=
"
java.lang.String
"
length
=
"
10
"
not
-
null
=
"
true
"
/>
<
property name
=
"
title
"
column
=
"
title
"
type
=
"
java.lang.String
"
length
=
"
10
"
not
-
null
=
"
true
"
/>
<
property name
=
"
created
"
column
=
"
created
"
type
=
"
java.util.Date
"
not
-
null
=
"
true
"
/>
<
property name
=
"
content
"
column
=
"
content
"
type
=
"
java.sql.Clob
"
not
-
null
=
"
true
"
/>
</
class
>
</
hibernate
-
mapping
>
记得要加上 polymorphism
=
"
explicit
" 表示为多态!
这样子,当我们需要加载大对象content的时候可以
session.load(Article.class,articleId);
OR
String hql="select a from Article as a where...";
如果,不需要用到大对象,则可以
session.load(ArticleInfo.class,articleId);
OR
String hql="select a from ArticleInfo as a where ...";
posted on 2005-06-12 22:44
一辉
阅读(158)
评论(0)
编辑
收藏
所属分类:
Hibernate
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理