延迟加载一类的 Blob 属性
我想要延迟加载 @Lob 属性。 首先,我使用 javassist 来检测我的类,如下所述 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties: 代码:
我的类包含“摘要”和“标题”属性,这些属性是 Lob 和其他属性。 代码:
public class News extends BaseEntity{
.
.
.
@Lob
@Basic(fetch = FetchType.LAZY)
public String getSummary() {
return summary;
}
@高球
@Basic(fetch = FetchType.LAZY)
公共字符串 getTitle() {
返回标题;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getPublishDate() {
return publishDate;
}
。
。
。
}
首先,我从数据库加载一条新闻,并想要检索新闻的发布日期(我在下面编写我的代码) 代码:
newsDAO.findByid(1L).getPublishDate();
findByid 方法是:
Code:
public News findById(Long id) throws ServiceException {
News entity = em.getReference(entityClass, id);
return entity;
}
然后,hibernate 生成这个查询: 代码:
Hibernate:
select
news0_.id as id1_,
news0_.entityVersion as entityVe2_1_,
news0_.publishDate as publish15_1_,
news0_.url as url1_
from
News news0_
where
news_.id=?
This query shows that ,it does not retrieve Lob property and fortunately lazy loading of Lob properties works well.但是当我只加载新闻的“摘要”属性时 代码:
newsDAO.findByid(1L).getSummary();
然后,hibernate 生成这些查询: 代码:
Hibernate:
select
news0_.id as id1_,
news0_.entityVersion as entityVe2_1_,
news0_.publishDate as publish15_1_,
news0_.url as url1_
from
News news0_
Hibernate:
select
news_.summary as summary1_,
news_.title as title1_
from
News news_
where
news_.id=?
我有两个问题: 1.我只想检索“summary”属性而不是“title”属性,但hibernate查询显示它也检索“title”属性,为什么会发生这种情况? 2.为什么hibernate会生成两个查询来仅检索新闻的摘要属性?
如果有人帮助我,我将不胜感激。 科斯罗。
I want to lazy load of @Lob properties.
First ,i use javassist to instrument my class as described here http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties:
Code:
My class contains "summary" and "title" properties those are Lob and other properties.
Code:
public class News extends BaseEntity{ . . . @Lob @Basic(fetch = FetchType.LAZY) public String getSummary() { return summary; }
@Lob @Basic(fetch = FetchType.LAZY) public String getTitle() { return title; }
@Temporal(TemporalType.TIMESTAMP) public Date getPublishDate() { return publishDate; }
.
.
.
}
First i load one news from database and want to retrieve publishdate of news(i write my codes in below)
Code:newsDAO.findByid(1L).getPublishDate();
and findByid method is :
Code: public News findById(Long id) throws ServiceException { News entity = em.getReference(entityClass, id); return entity; }
then ,hibernate generates this query:
Code:Hibernate: select news0_.id as id1_, news0_.entityVersion as entityVe2_1_, news0_.publishDate as publish15_1_, news0_.url as url1_ from News news0_ where news_.id=?
This query shows that ,it does not retrieve Lob property and fortunately lazy loading of Lob properties works well.But when i load only "summary" property of news
Code:newsDAO.findByid(1L).getSummary();
then ,hibernate generates these queries:
Code:Hibernate: select news0_.id as id1_, news0_.entityVersion as entityVe2_1_, news0_.publishDate as publish15_1_, news0_.url as url1_ from News news0_ Hibernate: select news_.summary as summary1_, news_.title as title1_ from News news_ where news_.id=?
I have two qurestions:
1.I only want to retrieve "summary" property not "title" property,but hibernate query shows that it also retrieve "title" property,Why this happens?
2.Why hibernate generates two query for retrieving only summary property of news?I would be appreciate if anyone helps me.
Khosro.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否启用了“延迟获取属性”?与其他延迟获取不同,默认情况下它是禁用的
在你的代码中,你调用了 2 个方法,所以我猜 hibernate 会生成 2 个查询。
findById 方法是什么样的?
Did you enable "lazy fetching for propery"? Unlike other lazy fetching, it is disabled by default
In your code, you called 2 method, so I guess hibernate generates 2 queries.
How does the findById method look like?
我的解决方案是创建一个单独的 Blob 实体(只有一个 id + 数据字段)并与您的实体“一对一”引用它,这样您就可以毫无问题地延迟加载它
My Solution is to make a separate Blob entity (which has just an id + data field) and reference it "one to one" with your Entity, so you can lazy load it without Problems