开发者

Parent-Child lazy get of child ids

I have a Parent-Child relationship where the Parent hasMany children and the the Child belongsTo the parent. The fetch mode is default (lazy). When I do a get of the Parent, sql equivalent to "select * from child where parent_id = ?" is also executed - that is, it gets every field of all the related children. (Is that lazy behavior?) I need to suppress the query of all the fields of the ch开发者_JS百科ildren, since there are clobs in there that I don't want to get unless I expressly get the child by id. How can I effect this? Do I have to create another "summary" domain child class that doesn't have the clob properties in it?


I think you figured out the lazy loading issue, so I won't comment on that.

The way I handle the issue of only getting certain data out of the children is to write a custom hql query that only returns the values you are interested in. So if you are going to need only the ids of the children do some sort of query like

Child.findAll("select c.id from Child c where parent = :parent", [parent: theParent])

See http://grails.org/doc/latest/ref/Domain%20Classes/executeQuery.html.


Do you know for a fact that it is getting them all at once? I ask this because, once you start peeking into the data using a debugger, println, logging, in order to show you the contents, Grails thinks you have requested that data, and will go fetch it. So it is probably lazy loaded, but you're telling Grails to go get it while you are trying to verify it is not being fetched.


Ah but, I see the point. The default view for "show" is accessing the child properties. Now, I also see the same behavior when rendering as XML or JSON, but that may be a similar problem - the child property is being interrogated?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜