jpql / criteria api中的递归查询

时间:2019-05-14 13:21:27

标签: java sql

我有这个SQL查询:

WITH RECURSIVE primaries(uid, other_field, parent_uid, parent_other_field) 
AS (
  SELECT uid, other_field, parent_uid, parent_other_field 
  FROM table 
  WHERE cast(uid as text) = 'string_uid'
  UNION
  SELECT tab.uid, tab.other_field, tab.parent_uid, tab.parent_other_field 
  FROM table tab 
     INNER JOIN primaries prim ON (cast(prim.uid as text) = tab.parent_uid 
                               or tab.parent_other_field = prim.other_field)
) 
SELECT * 
FROM primaries

如何在标准api或jpql中编写它? 在我的数据库中,此查询工作正常,但在我的Java代码(createNativeQuery)中,此查询不起作用。

uid       | other_field | parent_uid | parent_other_field
123-34-.. | test1       | null       |  null
345-23-.. | test2       | 123-34-..  |  null
854-54-.. | test3       | 345-23-..  |  null

当然,我的'string_uid'是123-34-..,我想查找以上所有记录。

0 个答案:

没有答案