2010-12-10 41 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