构造函数内具有多对一集合的 SELECT NEW() - HQL

发布于 2024-10-07 09:01:05 字数 769 浏览 0 评论 0原文

我似乎没有找到我拒绝接受的“不可能”的答案:)

这是我的 HQL 查询:

SELECT new TestTable(t.id,t.param1,t.param2,t.param3,stps) 
FROM TestTable t left join t.steps as stps 
WHERE t.someObj.id IN (:someObjIds)

TestTable 有以下构造函数:

public TestTable(Integer param1, Integer param2, Date param3, Date param4, Set steps)

我尝试在构造函数中使用 Collection 而不是 Set,但它没有不起作用,构造函数将仅接收集合中的第一项作为参数,而不是我预期的整个集合。

在查询中,我还尝试使用 left join fetch t.steps,尝试完全不使用 left join,尝试用“elements”包围构造函数中的“stps”参数,如下所示: elements(stps)

但没有任何效果。我这样做的原因是因为 TestTable 非常大并且有很多列和关联,但在本例中我只需要 4 列和一个集合。当一个查询最多可以返回 400,000 个对象时,这就变得有必要了。

有人有什么想法吗?

I don't seem to find an answer to something that I refuse to accept as "Not Possible" :)

Here is my HQL query:

SELECT new TestTable(t.id,t.param1,t.param2,t.param3,stps) 
FROM TestTable t left join t.steps as stps 
WHERE t.someObj.id IN (:someObjIds)

TestTable has the following constructor:

public TestTable(Integer param1, Integer param2, Date param3, Date param4, Set steps)

I have tried to use Collection in constructor instead of a Set but it didn't work, the constructor will receive only the first item out of the collection as a parameter and not the entire collection as I expected.

In the query I also tried to use left join fetch t.steps, tried without left join at all, tried to surround the "stps" parameter in the constructor with "elements" like this: elements(stps)

But nothing worked. The reason I'm doing this is because TestTable is very big and has a lot of columns and associations but in this case I want only 4 columns and one collection. When one query can return up to 400,000 objects this becomes necessary.

Any ideas anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2024-10-14 09:01:05

您不能在构造函数表达式中使用集合值路径表达式。按照答案

You can't use a collection-valued path-expression in a constructor expression. Follow answer

多像笑话 2024-10-14 09:01:05

您的查询可能在没有“新”构造函数的情况下工作,但它不会给您所期望的结果。

此查询返回 t.id,t.param1,t.param2,t.param3, [步骤表中的一个实体]
在每一行上,并由于左连接到步骤表而重复您的结果。

我的建议是,首先创建一个从 TestTable 获取数据的查询。
然后

List<TestTable> resultList = "select new TestTable(t.id,t.param1,t.param2,t.param3) from TestTable where
                      t.someObj.id in (:someObjId)"

,创建查询,从每个测试表的步骤表中获取数据。

for (TestTable tt : resultList) {
    List<Steps> stepList = "select st from Steps st where st.testTable.id = :ttId";
    tt.setSteps(stepList);
}

Your query may work without 'new' constructor but it does not give you what you expect.

This query returns t.id,t.param1,t.param2,t.param3, [one entity from steps table]
on each line and duplicates your results due to left join to steps table.

My recommendation is, firstly create a query that gets data from TestTable.
Something like

List<TestTable> resultList = "select new TestTable(t.id,t.param1,t.param2,t.param3) from TestTable where
                      t.someObj.id in (:someObjId)"

Then, create queries that gets data from step table for each TestTable.

for (TestTable tt : resultList) {
    List<Steps> stepList = "select st from Steps st where st.testTable.id = :ttId";
    tt.setSteps(stepList);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文