我已经在grails中创建了一个域类,它按照代码中的提法映射到vehicleList。现在我想获得家庭id = 1的车辆列表。我可以使用familyInstance.vehicleList
来获取此信息。
但我想知道有没有其他方法可以解决这个问题。我尝试应用标准但createCriteria
和withCriteria
在我提到arrayIndexOutOfBound
的投影中显示错误property("vehicleList")
错误。应用基本的MySql查询我可以直接从映射表中获取数据但是如果我想使用条件获取这些数据如何做到这一点。
class Family {
String name
Integer noOfVehicle
List vehicleList
static hasMany = [vehicleList: String]
static constraints = {
}
}
class FamilyController {
def edit(Family familyInstance) {
def fi = familyInstance
println "familyInstance ::: " + familyInstance
//Try 1
def familyCriteria = Family.createCriteria()
def vehicleList = familyCriteria.list{
createAlias('vehicleList','vlist')
projections {
property('vehicleList')
}
eq('id',familyInstance.id)
}
println "vehicleList ::: " + vehicleList
//Try 2
def vehicleList1 = familyInstance.vehicleList
println "vehicleList1 ::: " + vehicleList1
//Try 3
def vehicleList2 = Family.withCriteria{
createAlias('vehicleList','vlist')
projections{
property('vehicleList')
}
eq('id',familyInstance.id)
}
println "vehicleList2 ::: ===------>>>>>" + vehicleList2
respond familyInstance
}
}
数据库结构:
答案 0 :(得分:0)
删除createAlias
声明
Family.createCriteria().list{
projections {
property('vehicleList')
}
eq('id', familyInstance.id)
}
作品