【发布时间】:2013-12-30 16:19:30
【问题描述】:
我正在使用 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 支持这个吗?如果是,那么正确的语法是什么?
非常感谢。
【问题讨论】:
-
您是否也尝试过使用
createCriteria而不是withCriteria? -
是的,我有,没有其他事情发生。据我所知,如果条件包含投影,Grails 不支持直接将结果数据转换为域类。所以我认为在这种情况下我应该手动实现它。
-
如果你愿意,你可以根据你的需要定制一个ResultTransformer。这是 JAVA 中的 example。
标签: grails grails-orm createcriteria