2011-10-04 24 views
0

我有以下问题 - 仅给定一整个域环境的片段,我会试着解释它:排序为空的属性在:1间的关系

我有这两个领域类,并希望得到一份研究清单,并按他们(当前)医师的名字排序。我的问题是,我不知道该怎么做格姆的标准查询...

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

查询是没有问题的:一个MSSQL数据库

+0

我假设你得到某种异常? –

+0

可能是相关的:http://stackoverflow.com/questions/5708735/how-do-i-sort-by-a-property-on-a-nullable-association-in-grails –

回答

0

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 

我使用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

为什么有两个“命令”是吗? 我仍然收到一个异常,无法从研究对象中解析属性“currentPhysician”: org.hibernate.QueryException:无法解析属性:currentPhysician of:my.project.Study – limepix

相关问题