gpt4 book ai didi

sql - JPQL构造函数表达式,如何热切地获取 'select new'中的主要实体

转载 作者:行者123 更新时间:2023-12-04 08:56:22 30 4
gpt4 key购买 nike

我所拥有的原始查询有些复杂,但是我想做的是获取实体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"), //

问题是Hibernate仅选择AlertCondition的ID,因此在访问该对象时,这最终需要N + 1个选择,而我只想做1个。

根据调试,选择仅获取ID列:
select alertcondi0_.ID as col_0_0_, alertdefin1_.ID as col_1_0_, resource2_.ID as col_2_0_

我想找回的是* AlertCondition的所有字段。

我在Hibernate下找不到任何方法可以做到这一点。 JOIN FETCH在这里也不起作用。

另一种选择是选择表中的每一列,我想避免这种情况。

最佳答案

面对同样的问题。
我不知道如何让 hibernate 都无法获取ac.*

但是我找到了解决方法。

@Query("select ac, xxx, zzz")
List<Object[]> fetchRecords()

//
Constructor<?> constructor = AlertConditionEventCategoryComposite.class.getDeclaredConstructors()[0];
List<AlertConditionEventCategoryComposite> composites = fetchRecords.stream().map(constructor::newInstance).collect(Collectors.toList());

关于sql - JPQL构造函数表达式,如何热切地获取 'select new'中的主要实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506988/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com