7

我一直在尝试HQLCriteria的不同组合,但我无法避免一些不必要的连接(在两者中)和一些不必要的选择(在 Criteria 中)。

在我们的场景中,我们在SegmentApplication实体之间有一个@ManyToMany关系(导航是从 Segment 到 Applications)。

首先我尝试了这个标准

Application app = ...
List<Segment> segments = session.createCriteria(Segment.class)
    .createCriteria(Segment.APPLICATIONS)
    .add(Restrictions.idEq(app.getId()))
    .list();

Wich 产生这个 SQL:

select
    this_.id as id1_1_,
    this_.description as descript2_1_1_,
    this_.name as name1_1_,
    applicatio3_.segment_id as segment1_1_,
    applicatio1_.id as app2_,               <==== unnecessary APPLICATIONS columns
    applicatio1_.id as id7_0_,
    applicatio1_.name as name7_0_,
    applicatio1_.accountId as accountId7_0_,
    applicatio1_.applicationFlags as applicat5_7_0_,
    applicatio1_.description_ as descript6_7_0_,
from
    SEGMENTS this_ 
inner join
    SEGMENTS_APPLICATIONS applicatio3_ 
        on this_.id=applicatio3_.segment_id 
inner join                                       <==== unnecessary join
    APPLICATIONS applicatio1_ 
        on applicatio3_.app_id=applicatio1_.id 
where
    applicatio1_.id = ?

如您所见,Criteria 从 APPLICATIONS 中选择列,我不想被选中。我还没有找到办法(有可能吗?)。此外,它与 APPLICATIONS 连接,我认为这不是必需的,因为应用程序 id 已经在连接表 SEGMENTS_APPLICATIONS 中(HQL 也是如此)。

(作为另一个疑问,我想知道直接使用应用程序的限制,而不是 app.getId()。如您所见,我可以在查询的 HQL 版本中做到这一点)

由于我无法限制选择部分(我不需要应用程序属性),我尝试使用“select”子句使用此HQL :

Application app = ...
List<Segment> segments = session.createQuery(
    "select s from Segment s join s.applications as app where app = :app")
    .setParameter("app", app)
    .list();

产生:

select
    segment0_.id as id1_,
    segment0_.description as descript2_1_,
    segment0_.name as name1_,
from
    SEGMENTS segment0_ 
inner join
    SEGMENTS_APPLICATIONS applicatio1_ 
        on segment0_.id=applicatio1_.segment_id 
inner join                                        <==== unnecessary join
    APPLICATIONS applicatio2_ 
        on applicatio1_.app_id=applicatio2_.id 
where
    applicatio2_.id=? 

您可以看到HQL 没有从 Application 中选择属性(感谢“select s”部分),但仍然加入了 APPLICATIONS表,我认为这是不必要的。我们怎样才能避免呢?

(作为旁注,请注意在 HQL 中我可以直接使用 app,而不是 app.getId() 就像在 Criteria 中那样)

你能帮我找到一种方法来避免标准中的“选择”和标准和 HQL 中不必要的“连接”吗?

(此示例适用于 @ManyToMany,但我认为 @OneToMany 以及 @ManyToOne 和 @OneToOne 也会发生这种情况,即使 fetch = LAZY 也是如此)。

非常感谢,费兰

4

1 回答 1

6

使用 Criteria 时额外选择的列来自Hibernate 中的一个长期存在的错误。AFAIK,避免它的唯一方法是使用 HQL 或 JPA2 标准 API。

另一个问题也被标记为bug,但它的影响较小,我不会太在意。

于 2012-05-19T12:34:55.607 回答