【问题标题】:Question of grails findAllBy when search for for foreign key搜索外键时grails findAllBy的问题
【发布时间】:2010-12-10 03:32:29
【问题描述】:
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 grails-orm findall


    【解决方案1】:

    我不认为你可以通过动态查找器来做到这一点,但你应该可以使用 Hibernate 来做一个 createCriteria

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

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

    【讨论】:

    • 谢谢。 > 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),这就是我需要的:来自 Info_File this_ where this_.parent_id=?
    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2013-11-23
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多