Download queries with GORM / Hibernate

My Grails has the following domain objects

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

class Attribute {       
    String name
    static belongsTo = [productType: ProductType]
} 

My DB has 7 ProductType, and each of them has 3 Attributes. If I execute the request:

def results = ProductType.withCriteria {
    fetchMode("attributes", org.hibernate.FetchMode.EAGER)
}

I expect to return 7 instances ProductType, but actually I get 21 (7 x 3). I understand that if I execute the equivalent SQL query to the above, then in the result set there would be 21 rows

prod1 | attr1
prod1 | attr2
prod1 | attr3
..... | .....
..... | .....
prod7 | attr1
prod7 | attr2
prod7 | attr3
-------------
Total 21

But I thought that when I get these results through Hibernate / GORM, I should get something more:

prod1 | attr1, attr2, attr3    
..... | ...................
..... | ...................
prod7 | attr1, attr2, attr3
---------------------------
Total 7

By the way, if I remove the download from the request above, I get 7 ProductType, as expected. What am I missing?

+3
source share
1 answer

faq: Hibernate ( )?

, , , 7 * 3 , 7 productTypes ( 2 ).
, , (, sql- ):

SetResultTransformer(new DistinctRootEntityResultTransformer())

def results = ProductType.withCriteria {
    fetchMode("attributes", org.hibernate.FetchMode.EAGER)
    SetResultTransformer(new DistinctRootEntityResultTransformer())
}
+4

Source: https://habr.com/ru/post/1717888/


All Articles