2
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)などのメソッドはありますか

3倍


ありがとう。

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

1 に答える 1

4

動的ファインダーを介してそれを実行できるとは思いませんが、Hibernateを使用してcreateCriteriaを実行できるはずです。

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

おそらくこれが役立つかもしれません:https ://docs.grails.org/4.0.1/ref/Domain%20Classes/createCriteria.html

于 2010-12-10T05:04:43.227 に答える