gpt4 book ai didi

hibernate - 延迟加载一个类的 Blob 属性

转载 作者:行者123 更新时间:2023-12-05 01:36:01 25 4
gpt4 key购买 nike

我想延迟加载@Lob 属性。首先,我使用 javassist 来检测我的类,如此处所述 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties :代码:

我的类(class)包含“摘要”和“标题”属性,它们是 Lob 和其他属性。代码:


public class News extends BaseEntity{
.
.
.
@Lob
@Basic(fetch = FetchType.LAZY)
public String getSummary() {
return summary;
}<p></p>

<p>@Lob
@Basic(fetch = FetchType.LAZY)
public String getTitle() {
return title;
}</p>

<pre><code>@Temporal(TemporalType.TIMESTAMP)
public Date getPublishDate() {
return publishDate;
}
</code></pre>

<p>.
.
.
}
</p>

首先我从数据库中加载一个新闻并想要检索新闻的发布日期(我在下面写了我的代码)代码:


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=?
此查询表明,它不会检索 Lob 属性,幸运的是 Lob 属性的延迟加载效果很好。

但是当我只加载新闻的“摘要”属性时代码:


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会生成两个只检索新闻摘要属性的查询?

如果有人帮助我,我将不胜感激。科斯罗。

最佳答案

  1. 您是否启用了“延迟获取属性”?与其他延迟抓取不同,它默认是禁用的

  2. 在您的代码中,您调用了 2 个方法,所以我猜 hibernate 会生成 2 个查询。findById 方法是什么样子的?

关于hibernate - 延迟加载一个类的 Blob 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2478681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com