【发布时间】:2016-10-07 07:37:00
【问题描述】:
我有一个 JSF 数据表并显示一个包含延迟加载图像的实体。点击行查看详情后加载。
但我必须在数据表中显示图像计数。数据表使用分页,每页显示 15 行。
我使用以下注解进行映射
// Fetch type must be lazy due to out of memory error in eager mode
@OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE }, mappedBy = "alarm", fetch = FetchType.LAZY, orphanRemoval = true)
@OrderBy("unixtime ASC")
@LazyCollection(LazyCollectionOption.EXTRA) // allows .size() and .contains() without initializing the whole collection
private volatile Set<AlarmImage> alarmImages = new HashSet<AlarmImage>();
用这种方法获取图片数量
/**
* @return the number of alarm images in this alarm
*/
public Integer getCountAlarmImages() {
// wont initalize whole collection because LazyCollectionOption.EXTRA
return this.alarmImages.size();
}
这是休眠日志
[2016-10-07 08:51:13,095] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,190] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,282] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,375] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,466] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,559] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,651] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,744] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,836] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,930] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,022] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,117] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,210] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,302] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,395] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
我的问题是分页平均需要 1.5 秒才能加载,是否可以对此进行优化?也许并行加载每个警报的图像数量?这在 jsf 数据表中可以吗?
编辑:
休眠版本:4.2.17.Final
xhtml 文件中的行定义
<p:column>
<h:outputText value="#{alarm.countAlarmImages}" />
</p:column>
【问题讨论】: