Grails 2.4.4. hierarchical domain structure: find

132 views Asked by At

I have hierarchical folder structure:

class Folder {
    String name;
    List<Folder> subfolders;

I want to select all Folders, which are not subfolder. How can I write findAll method/query in jpql like :

SELECT * FROM folder WHERE folder.id NOT IN (
SELECT folder.id FROM folder INNER JOIN folder_folder ON folder.id = folder_folder.folder_id);

I don't have parent field;

something like this?

Folder.findAll("from Folder as folder where folder.id not in subfolders");
1

There are 1 answers

0
yazabara On

Need to change domain to one-to-many; Just add static mappedBy = [subLevels: "parentFolder"]

class Folder {
        String name;
        Folder parentFolder;
        List<Folder> subfolders;
        static mappedBy = [subLevels: "parentFolder"]

And findAll method will be

Folder.findAll("from Folder as folder where folder.parentFolder = null");