【发布时间】:2012-07-11 22:17:06
【问题描述】:
我有如下三个实体:
public class EntityA
{
private Long id;
//Getters and setters
}
public class EntityB
{
private Long id;
private EntityA entitya;
//Getters and setters
}
public class EntityC
{
private Long id;
private BigDecimal amount;
private EntityB entityb;
//Getters and setters
}
现在,给定 EntityA 的实例,我想获取 EntityC 的列表。我目前有两种选择。不知道哪个更优化。选项有:
1.
select c from EntityC c where c.entityb in (select b from EntityB b where b.entitya = :entitya)
2。 向 EntityB 添加新属性
private Set<EntityC> entityCCol;
@OneToMany(mappedBy="entityb")
public Set<EntityC> getEntityCCol()
{
return entityCCol;
}
select b from EntityB a join fetch a.entityCCol b
这两个查询中哪一个更容易和优化?
【问题讨论】:
-
select c from EntityC c where c.entityB.entityA = :a怎么样?如果您创建此查询,create index IX_B_A on entityB(entityA, id)生成的查询将足够快。
标签: java jpa persistence jpa-2.0