我有hibernate查询性能的问题,我无法弄清楚.在下面的代码片段中,我需要选择具有至少一个映射和过滤映射的实体.我正在使用FETCH JOIN来加载仅过滤的映射.但在这种情况下,我的查询存在性能问题.Hibernate说警告日志:
org.hibernate.hql.ast.QueryTranslatorImpl - 使用collection fetch指定的firstResult/maxResults; 在记忆中应用!
当我省略FETCH JOIN并且只剩下JOIN查询时很快.但结果我将所有映射加载到实体,这对我来说是不可接受的状态.有没有办法提高查询性能?映射表中有很多行.
HQL查询:
select distinct e from Entity
join fetch e.mappings as mapping
where e.deleted = 0 and e.mappings is not empty
and e = mapping.e and mapping.approval in (:approvals)
Run Code Online (Sandbox Code Playgroud)
实体:
@Entity
@Table(name="entity")
class Entity {
...
@OneToMany(mappedBy="entity", cascade=CascadeType.REMOVE, fetch=FetchType.LAZY)
@OrderBy("created")
private List<Mapping> mappings = new ArrayList<Mapping>();
...
}
@Entity
@Table(name="mapping")
class Mapping {
public static enum MappingApproval {
WAITING, // mapping is waiting for approval
APPROVED, // mapping was approved
DECLINED; // mapping was declined
}
...
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="entity_id", nullable=false)
private Entity entity;
@Enumerated(EnumType.STRING)
@Column(name="approval", length=20)
private MappingApproval approval;
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢
| 归档时间: |
|
| 查看次数: |
15699 次 |
| 最近记录: |