JPQL构造函数表达式,如何在'select new'中急切地获取主实体

use*_*644 7 sql select hibernate jpa

我的原始查询有点复杂,但我要做的是获取实体AlertCondition以及一些其他字段.

    + "  SELECT new org.rhq.core.domain.alert.composite.AlertConditionEventCategoryComposite " //
    + "       ( " //
    + "         ac, " //
    + "         res.id " //
    + "       ) " //
    + "    FROM AlertCondition AS ac FETCH ALL PROPERTIES " //
    + "    JOIN ac.alertDefinition ad " //
    + "    JOIN ad.resource res " //
    + "   WHERE " + AlertCondition.RECOVERY_CONDITIONAL_EXPRESSION //
    + "     AND ( res.agent.id = :agentId OR :agentId IS NULL ) " //
    + "     AND ad.enabled = TRUE " //
    + "     AND ad.deleted = FALSE " //
    + "     AND ac.category = 'EVENT' " //
    + "ORDER BY ac.id"), //
Run Code Online (Sandbox Code Playgroud)

问题是Hibernate只选择AlertCondition的ID,所以当访问这个对象时,这最终需要N + 1选择,而我只想做1.

根据debug,select只获取ID列:

select alertcondi0_.ID as col_0_0_, alertdefin1_.ID as col_1_0_, resource2_.ID as col_2_0_
Run Code Online (Sandbox Code Playgroud)

我想要回来的是*AlertCondition的所有领域.

我在Hibernate下找不到任何办法.JOIN FETCH在这里也没有真正的工作.

另一种方法是选择表格的每一列,我想避免.

jau*_*udo -1

您可以告诉 hibernate 获取广告:

JOIN FETCH ac.alertDefinition ad
Run Code Online (Sandbox Code Playgroud)