Hibernate查询(比较总和) - 两个不同数据源的grails

时间:2012-07-30 04:07:55

标签: hibernate grails hql gorm

我有这个代码,工作正常。我只是想问一下,如果我使用 executeQuery ,比如在子查询中比较sum,我是否也可以这样做,我认为使用 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是&gt; StudentTestRoomExamSchedule中的学生。 2)ExamSchedule.actualExamDateTime是&lt; = now。

我有这些域类:

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 的原因。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用两种不同的数据源,则可能无法实现。

我只能想象一些非常狭隘的情况,即在单个查询中可以进行数据库间操作。

相关问题