I have a table folder with (id, name, parent_id ) columns. The folder which is parent it's parent_id = -1. I need to get top level parent Id of each folder. The following query works fine, but it doesn't work with spring data jpa.
WITH RECURSIVE nodes_cte AS (
SELECT tn.id, tn.parent_id, tn.name, tn.id top_id
FROM folder AS tn
WHERE tn.parent_id =-1
UNION ALL
SELECT c.id, c.parent_id, c.name, p.top_id
FROM nodes_cte AS p, folder AS c
WHERE c.parent_id = p.id
)
SELECT distinct * FROM nodes_cte
How can I create postgresql function which will return same result as above query?