通过Grails 3中的WHERE子句筛选出多个结果

时间:2018-01-30 21:08:32

标签: hibernate grails

我有像这样的Grails Domain Class

class ClassRoom {

    Integer Id
    String classRoomName
    Date dateDeleted

    static hasMany = [students: Student]

}

class Student {
     String studentName
     Date dateDeleted
}

假如我从ClassRoom打印classRoom 1的所有学生。 println ClassRoom.findByIdAndDateDeletedIsNull(1).students它将打印所有有dateDeleted的学生。 有没有办法过滤掉dateDeleted SET的记录?

2 个答案:

答案 0 :(得分:2)

以下将返回学生名单

ClassRoom.executeQuery("""
        select s 
          from ClassRoom c
          join c.students s
         where c.id = :id 
           and c.dateDeleted is null
           and s.dateDeleted is null
""", [id: id])

答案 1 :(得分:1)

过滤结果:

ClassRoom.findByIdAndDateDeletedIsNull(1).students.findAll{!it.dateDeleted}