我有这个代码,工作正常。我只是想问一下,如果我使用executeQuery来比较子查询中的总和,这是否也是可能的,我认为使用executeQuery进行查询将比我现有的代码处理得更快。这是我的代码:
def availableSched = []
def tres = TestRoomExamSchedule.getAll()
tres.each { t ->
def stres = StudentTestRoomExamSchedule.countByTestRoomExamSchedule(t)
if (stres < t.testRoom.totalTestStations && t.examSchedule.actualExamDateTime <= new Date()) {
availableSched.add(t)
}
}
return availableSched
这个返回一个满足以下条件 的TestRoomExamSchedule列表: 1)TestRoom.totalTestStations is > student,它在 StudentTestRoomExamSchedule 中。2)ExamSchedule.actualExamDateTime <= 现在。
我有这些域类:
class TestRoom {
String code
String name
int totalTestStations
static constraints = {
}
static mapping = {
datasource 'admin'
table 'testroom'
code column: 'code'
name column: 'name'
totalTestStations column: 'totaltestmach', sqlType: "smallint"
}
TestRoomExamSchedule 域类
class TestRoomExamSchedule implements Serializable{
Long testRoomId
ExamSchedule examSchedule
TestRoom testRoom
static transients = ['testRoom']
static constraints = {
}
static mapping = {
table 'testroom_examschedule'
version false
id generator: 'assigned', composite: ['testingCenterId','examSchedule']
testRoomId column: 'testingcenter_id'
examSchedule column: 'examschedule_id'
}
StudentTestRoomExamSchedule 域类
class StudentTestRoomExamSchedule implements Serializable {
Student student
TestRoomExamSchedule testRoomExamSchedule
static constraints = {
}
static mapping = {
table 'person_examschedule'
version: false
id composite: ['student', 'testRoomExamSchedule']
student column: 'person_id'
columns {
testingCenterExamSchedule {
column name: 'testroom_id'
column name: 'examschedule_id'
}
}
}
我正在使用两个不同的数据源,这就是为什么我需要在我的域类TestRoomExamSchedule中以瞬态声明属性testRoom的原因。提前致谢。