Hibernate - 用作表达式的子查询返回的多行

Hibernate - more than one row returned by a subquery used as an expression

我有一个场景,可以根据[=]的公共id从三个(Table1, Table2, Table4) table中提取信息41=]1

为了从 Table 4 中提取信息,我必须寻找另一个中间体 table(Table3) 以获得 Table 3 和 [=41] 之间的公共 ID =] 4. Table 4条信息的查询结果将包含多行作为查询响应。

我已经为我的需求编写了以下 HQL(Hibernate Query)

select t1,t2,(select t4.column1 from Table4 t4 
join Table3 t3 on t4.column0=t3.column0 and t3.column1=t1 and t3.column2='desired_value') from Table1 t1 
join Table2 t2 on t2.column1=t1.column1 
where t1.column0=:id and t1.column3=:value

我有所有四个 table 的实体 class。

如何select 子查询中的多行

Spring Data JPA 中 POSTGRESQL array_agg() / string_agg() 方法的等效项是什么?

没有子查询,我可以使用我的实体 class 对象获取 Object[] 响应。

如何添加我的子查询以便在单个事务中检索所有我想要的数据,而无需在我的存储库中创建单独的查询方法class?

到目前为止我尝试了什么:

select t1,t2,(select new List(t4.column1) from Table4 t4 
join Table3 t3 on t4.column0=t3.column0 and t3.column1=t1 and t3.column2='desired_value') from Table1 t1 
join Table2 t2 on t2.column1=t1.column1 
where t1.column0=:id and t1.column3=:value

下面是我要实现的输出对象:

List<Object[]> as my query response 

         where each Object[] will be of size 3 with following

                 Object[0] -> Table1_Entity object
                 Object[1] -> Table2_Entity object
                 Object[2] -> will be a String[] containing a specific column values from Table4 Entity class

即响应 = [[obj1, obj2, ["value1, value2","value3"]], [obj3, obj4, ["value1, value2","value3"]], ..... .]

试试这个:

select t1,t2, (select cast(function('string_agg', t4.column1, ', ') as string) from Table4 t4 
join Table3 t3 on t4.column0=t3.column0 and t3.column1=t1 and t3.column2='desired_value') from Table1 t1 
join Table2 t2 on t2.column1=t1.column1 
where t1.column0=:id and t1.column3=:value