2010-12-10 4 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을 보내드립니다, 1 내가 원하는 해달라고 부모 파일 정보를 얻을 수 있습니다.

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

답변

3

시도,하지만 당신은 createCriteria

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

를 할 최대 절전 모드를 사용할 수 있어야합니다 : http://www.grails.org/Hibernate+Criteria+Builder

+0

감사합니다. > 부모 {당량 ('키'는, 1)} SQL (? parent_ali1_.id =) 왼쪽 외부 this_ INFO_FILE에서 는 this_.parent_id = parent_ali1_.id 여기서 에 INFO_FILE의 parent_ali1_ 가입하지만 테이블을 가입해야 말아. 그래서 이퀄라이저 ('parent.id', 1L)를 시도해 봅니다. 내가 필요로하는 것 : Info_File this_ where this_.parent_id =? – atian25