Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GORM/Hibernate快速加载查询_Hibernate_Grails_Gorm - Fatal编程技术网

使用GORM/Hibernate快速加载查询

使用GORM/Hibernate快速加载查询,hibernate,grails,gorm,Hibernate,Grails,Gorm,My Grails应用程序具有以下域对象 class ProductType { String name static hasMany = [attributes: Attribute] } class Attribute { String name static belongsTo = [productType: ProductType] } My DB有7个ProductTypes,每个都有3个Attributes。如果我执行查询: def

My Grails应用程序具有以下域对象

class ProductType {
    String name
    static hasMany = [attributes: Attribute]
}

class Attribute {       
    String name
    static belongsTo = [productType: ProductType]
} 
My DB有7个
ProductType
s,每个都有3个
Attribute
s。如果我执行查询:

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
顺便说一句,如果我从上面的查询中删除渴望加载,我会得到7个
ProductType
s。我遗漏了什么?

你应该读一下

当您指定即时加载时,正如您所注意到的,结果集包含7*3行,但实际上内存中只有7个productTypes对象(每个对象额外引用2个)。
要执行所需操作,可以添加(请注意,基础sql查询没有更改):

SetResultTransformer(新的distinctroentityResultTransformer())


我自己也注意到了这一点,但那是在我使用Grails1.0.4时,您能指定您使用的Grails版本吗?
def results = ProductType.withCriteria {
    fetchMode("attributes", org.hibernate.FetchMode.EAGER)
    SetResultTransformer(new DistinctRootEntityResultTransformer())
}