我的 Grails 应用程序具有以下域对象
class ProductType {
String name
static hasMany = [attributes: Attribute]
}
class Attribute {
String name
static belongsTo = [productType: ProductType]
}
我的数据库有 7ProductType
秒,每个都有 3Attribute
秒。如果我执行查询:
def results = ProductType.withCriteria {
fetchMode("attributes", org.hibernate.FetchMode.EAGER)
}
我希望返回 7 个实例ProductType
,但实际上我得到 21 (7 x 3)。我知道如果我要执行与上述等效的 SQL 查询,结果集将有 21 行
prod1 | attr1
prod1 | attr2
prod1 | attr3
..... | .....
..... | .....
prod7 | attr1
prod7 | attr2
prod7 | attr3
-------------
Total 21
但我认为,当我通过 Hibernate/GORM 检索这些结果时,我应该得到更像:
prod1 | attr1, attr2, attr3
..... | ...................
..... | ...................
prod7 | attr1, attr2, attr3
---------------------------
Total 7
顺便说一句,如果我从上面的查询中删除急切加载,我会得到ProductType
预期的 7 秒。我错过了什么?