Implement support for native recursive query functionality of popular DBMSes

Description

We have a couple of areas in our system where need to load tree structures from our database. These trees can be very deep (often 20+ levels). SQL Server 2005 introduced Common Table Expressions, a kind of in-line view that can be used recursively.

These allow us to quickly get a result set like this:

id, label, parentId
1, foo, null
2, foo2, 1
3, foo3, 2
4, foo4, 2
5, foo5, 4
6, foo6, 1

From a tree like this:

[1, foo]

-[2, foo2]

 

  • [3, foo3]

 

  • [4, foo4]

 

  • [5, foo5]

  • [6, foo6]

using a query like this (not tested):

with MyCTE(
id,
label,
parentId)
as
( select n.id, n.label, n.parentid
from Node n
UNION ALL
select c.id, c.label, c.parentId
from MyCTE c
inner join Node n on n.id=c.parentId)
select * from MyCTE

It happens many times more quickly than we've been able to load the graphs with Hibernate, even using batching etc.

We've tried using hibernate's built in native sql support without success. The alias injection breaks the Common Table Expression definition ( with MyCTE ), as it is a view definition and doesn't allow for the 'id as id_276' syntax, rather requiring just a column name.

I know that there are several other major DBMSes that support recursive querying now. Is there a way to have support for this functionality in hibernate core?

Activity

Show:

Christian Beikov June 14, 2022 at 9:24 AM

This will be done as part of adding support for the WITH clause as part of https://hibernate.atlassian.net/browse/HHH-15328.

Jan-Willem Gmelig Meyling September 7, 2016 at 1:51 AM
Edited

Vote from my side of this issue! Christian, do you have a small example of how your library plays together with Hibernate to do such a query?

Seems by the way it has been done once: Extending HQL with Plain Recursive Facilities (Aneta Szumowska et al.) http://link.springer.com/chapter/10.1007%2F978-3-642-32741-4_24

Christian Beikov June 29, 2016 at 1:25 PM

I don't want to advertise, but I implemented a library that works on top of hibernate which lets you use CTEs. I personally use it with PostgreSQL and DB2.

Bogdan Artyushenko October 31, 2013 at 12:59 PM

>>Environment is not only MSSQL but also Oracle and Postgresql.
And IBM DB2

Duplicate

Details

Assignee

Reporter

Priority

Created July 7, 2009 at 5:44 PM
Updated June 14, 2022 at 9:25 AM
Resolved June 14, 2022 at 9:24 AM