Search code examples
hibernategrailsgrails-ormfindall

Question of grails findAllBy when search for for foreign key


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)   
    }   
}

now i want to get all the file which parent id = 1 how can i do it?

i try to use

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

but it will send 2 sql, the 1st is to get the parent file info which i dont want it.

is there any method such as File.findAllByParentId(1L)

3x


thanks.

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

but i dont need to join table. so i try

eq('parent.id',1L)
//it's what i need:
//from Info_File this_ where this_.parent_id=?

Solution

  • I do not think that you can do it through the dynamic finders, but you should be able to use Hibernate to do a createCriteria

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

    Perhaps this may help: https://docs.grails.org/4.0.1/ref/Domain%20Classes/createCriteria.html