2011-10-04 6 views
0

私は次のような問題を持っている - 全ドメイン環境のちょうどスニペットを考えると、私はそれを説明してみましょう:ソート:1関係

私はこれらの2つのドメインクラスを持っているし、取得したいです研究リストを作成し、現在の医師の名前で並べ替えます。私の問題は...私はGORMと基準のクエリを実行する方法を知っているドントこと、

class Study {  
    Date studydate 
    String comment 
    static belongsTo = [currentPatient:Patient, originalPatient: Patient, originalPhysician: Physician, currentPhysician: Physician] 

    static mapping = { 
     columns{ 
      currentPatient column:'id_patient_current' 
      originalPatient column:'id_patient' 
      originalPhysician column:'id_physician' 
      currentPhysician column:'id_physician_current' 
     } 
    } 
} 

class Physician { 
    String personname 

    static hasMany = [currentStudies: Study, originalStudies: Study] 

    static mappedBy = [currentStudies: 'currentPhysician', 
         originalStudies: 'originalPhysician'] 
} 

であり、現在のクエリ:

def physician = Physician.get(params?.phId) 

def studies = Study.withCriteria{ 
    and{ 
     maxResults(limit as Integer) 
     firstResult(offset as Integer) 

     currentPhysician{ 
      if(physician){ 
       eq('id', physician.id) 
      }  
      order('personname', 'asc') 
     } 
    } 
} 

問題ではない、すべての研究がなければならないということです医師 - データベース内の列である "id_physician"と "id_physician_current"にNULL値が含まれている可能性があります(患者は医師から来ていません)。

直接SQL経由でのクエリは問題ありません:

select st.id, ph.personname from study as st left join Physician as ph on ph.ID = st.ID_Physician_Current order by ph.PersonName 

私は次のクエリは、あなたが求めているものを実現すると信じてMSSQLデータベース

+0

何らかの例外が発生していると思いますか? –

+0

関連性がある可能性があります:http://stackoverflow.com/questions/5708735/how-do-i-sort-by-a-property-on-a-nullable-association-in-grails –

答えて

0

上のGrails 1.3.7を使用しています。

def studies = Study.withCriteria{ 
    maxResults(limit as Integer) 
    firstResult(offset as Integer) 
    order('currentPhysician.personname', 'asc') 

    if(physician) { 
     or { 
      eq('currentPhysician', physician) 
      isNull('currentPhysician') 
     } 
    } 

    order('currentPhysician.personname', 'asc') 
} 

そうでないかもしれないあなたが期待まったく同じSQLとしてではなく、あなたは基準を書くべきではないことを心配している場合。

+0

なぜ2つの "注文"の? "currentPhysician"というプロパティがStudyオブジェクトから解決できないという例外がまだ出ています: org.hibernate.QueryException:プロパティを解決できませんでした:currentPhysician of:my.project.Study – limepix