JOOQ - Recursive Query

Viewed 186

I have the following database structure:

File

  • file_id
  • file_name
  • folder_id

Folder

  • folder_id
  • folder_name
  • parent_folder_id

I'm trying to dynamic build the file path this way:

Query: - This returns the file and the folder where is located.

dsl.select()
   .from( FILE)
   .join( FOLDER)
   .on( FILE.FOLDER_ID.eq( FOLDER.FOLDER_ID) )
   .fetch();

And then I get the parent folders in simple Java:

List<Folder> parentFolders= new ArrayList<>();
Long parentFolderId = record.get( FOLDER.PARENT_FOLDER_ID );
while ( parentFolderId != null )
{
    Folder parentFolder = folderService.get(parentFolderId );
    parentFolders.add( 0, parentFolder );
    parentFolderId = parentFolder.getParentFolderId();
}

Output would look like this for instance:

Folder -> Folder -> Folder -> File

To maybe be fetched in a class like this:

Long fileId;
String fileName;
Long fileId;
String fileName;
List<Folder> parents;

Is there any way to do this two steps in JOOQ? The idea is to write a query that does as well the lookup for the parent folders. Is this even possible just with JOOQ?

0 Answers
Related