Grails标准在联接表上的投影
我对grails criteria builder有一个问题,我想对与父表有一对多关系的表上的列进行投影示例:Grails标准在联接表上的投影,grails,alias,projection,criteria-api,Grails,Alias,Projection,Criteria Api,我对grails criteria builder有一个问题,我想对与父表有一对多关系的表上的列进行投影示例: Car.createCriteria() { projections { property('name') property('wheels.name')// ???? } join 'wheels' //or wheels {} ??? } 还是存在类似的情况?我认为这是别名的基本问题我假设以下域类: class Car {
Car.createCriteria() {
projections {
property('name')
property('wheels.name')// ????
}
join 'wheels'
//or wheels {} ???
}
还是存在类似的情况?我认为这是别名的基本问题我假设以下域类:
class Car {
String name
static hasMany = [wheels : Wheel]
}
class Wheel {
String name
static belongsTo = [car : Car]
}
我还假设这是期望的输出:
CarName WheelName
Car1 Wheel1
Car1 Wheel2
Car2 Wheel3
在这种情况下,您可以执行以下操作:
void testCarProjectionItg() {
def car1 = new Car(name: 'Car1').save()
def car2 = new Car(name: 'Car2').save()
def wheel1 = new Wheel(name: 'Wheel1')
def wheel2 = new Wheel(name: 'Wheel2')
def wheel3 = new Wheel(name: 'Wheel3')
car1.addToWheels wheel1
car1.addToWheels wheel2
car2.addToWheels wheel3
wheel1.save()
wheel2.save()
wheel3.save()
car1.save()
car2.save()
println Wheel.withCriteria {
projections {
property('name')
car {
property('name')
}
}
}
}
--Output from testCarProjectionItg--
[[Wheel1, Car1], [Wheel2, Car1], [Wheel3, Car2]]
在这种情况下,我更喜欢HQL查询:
println Wheel.executeQuery("select car.name, wheel.name from Car car inner join car.wheels wheel")
--Output from testCarProjectionItg--
[[Car1, Wheel1], [Car1, Wheel2], [Car2, Wheel3]]
只做以下事情怎么样:
Car.createCriteria().list() {
createAlias("wheels","wheelsAlias")
projections {
property('name')
property('wheelsAlias.name')
}
}
还是我遗漏了什么?非常好!在演示前几分钟帮了我很大的忙!