0

我正在使用 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 支持这个吗?如果是,那么正确的语法是什么?
太感谢了。

4

1 回答 1

0

实际上,在研究了许多文章、Grails 文档甚至它的源代码之后,我认为最好的方法是为我自己的目的实现一个自定义转换器。这里最困难的事情是如何将数据转换为关联对象并将它们收集到根实体内部的集合中。我在这里创建了一个: http
://www.mycodestock.com/code/10333/ 希望它能帮助你们可能需要像我这样的东西。

于 2014-01-15T17:08:07.870 回答