我有以下问题 - 仅给定一整个域环境的片段,我会试着解释它:排序为空的属性在: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数据库
我假设你得到某种异常? –
可能是相关的:http://stackoverflow.com/questions/5708735/how-do-i-sort-by-a-property-on-a-nullable-association-in-grails –