搜索外键时的grails findAllBy问题

问题描述 投票:2回答:1
class File {
   String name
   File parent;    
   static belongsTo =[parent:File ]   
   static hasMany = [childrens:File];   
   static mapping = {   
      table 'Info_File'
      id(generator:'sequence', params: [sequence: 'seq_file'])
      parent:[lazy:"true",cascade:"none"]   
      children joinTable:[name:'children', key:'parent_Id', column:'Id',lazy:"true",inverse:"false",cascade:"none"]   
    }   
    static constraints = {   
        parent(nullable:true)   
    }   
}

现在我想获取所有父ID = 1的文件我该怎么办?

我尝试使用

def fileList = File.findAllByParent(File.get(1L))

但是它将发送2 sql,第一个是获取我不想要的父文件信息。

是否有任何方法,例如File.findAllByParentId(1L)

3x


谢谢。

parent{eq('key', 1)} 
//sql:from Info_File this_ left outer join Info_File parent_ali1_ 
//on this_.parent_id=parent_ali1_.id where (parent_ali1_.id=?)

但是我不需要加入表。所以我尝试

eq('parent.id',1L)
//it's what i need:
//from Info_File this_ where this_.parent_id=?
hibernate grails gorm findall
1个回答
4
投票

我不认为您可以通过动态查找器来做到这一点,但是您应该能够使用Hibernate来执行createCriteria

File.createCriteria().list{
    parent{
        eq('key', 1)
    }
}

也许这可能会帮助:https://docs.grails.org/4.0.1/ref/Domain%20Classes/createCriteria.html

© www.soinside.com 2019 - 2024. All rights reserved.