我有三個實體如下:如何優化JPA查詢
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
這兩個查詢更容易和優化?
什麼關於'從EntityC c中選擇c,其中c.entityB.entityA =:a'?如果你創建了這個查詢,並且在entityB(entityA,id)上創建索引IX_B_A,那麼結果查詢就足夠快了。 – 2012-07-12 10:42:59