2010-12-10 37 views
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 我怎样才能做到这一点的文件吗?问题的Grails findAllBy搜索时为外键

我尝试使用

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

,但它会发出2 SQL,第一是让父文件信息,我不希望它。

是有诸如File.findAllByParentId(1L)


感谢的任何方法。

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=? 

回答

3

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

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

或许,这可以帮助:http://www.grails.org/Hibernate+Criteria+Builder

+0

谢谢。我的父亲{eq('key',1)} sql: from Info_File this_ left outer join Info_File parent_ali1_ on this_.parent_id = parent_ali1_.id其中(parent_ali1_.id =?) 但我不需要连接表。 所以我尝试eq('parent.id',1L),这是我所需要的: from Info_File this_ where this_.parent_id =? – atian25 2010-12-10 05:24:03