我正在使用 Grails 2.2.4 构建一个 Web 应用程序,现在我在使用 Grails withCriteria 操作时遇到了一个复杂的问题。我有 3 个课程如下:
class Person {
int id
String name
Set<Car> cars = [] // One-to-many relationship
Company currentCompany // One-to-one relationship
}
class Car {
int id
String manufacturer
String brand
double price
Person owner
}
class Company {
int id
String name
Set<Person> employees = []
}
现在我想从 Person 作为根类查询数据以及如下相关数据:
List result = Person.withCriteria {
projections {
property('id')
property('name')
createAlias('cars', 'c', CriteriaSpecification.LEFT_JOIN)
property('c.brand')
createAlias('currentCompany', 'com', CriteriaSpecification.LEFT_JOIN)
property('com.name')
}
lt('id', 10L)
resultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
}
问题是,我不知道如何将结果数据深度转换为人员列表,以确保每个元素都包含其数据作为类结构。我尝试了很多方法,比如
resultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
resultTransformer(CriteriaSpecification.aliasToBean(Person.class))
但没有像我预期的那样奏效。
Grails 2.2.4 支持这个吗?如果是,那么正确的语法是什么?
太感谢了。