Grails 2.4.4。分层域结构:查找

时间:2015-06-05 11:56:34

标签: grails gorm

我有分层文件夹结构:

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

我想选择所有不属于子文件夹的文件夹。 如何在jpql中编写findAll方法/查询,如:

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

我没有父字段;

这样的事情?

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

1 个答案:

答案 0 :(得分:1)

需要将域名更改为一对多; 只需添加static mappedBy = [subLevels:“parentFolder”]

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

并且findAll方法将是

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