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 children, 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?
链接地址: http://www.djcxy.com/p/24330.html上一篇: Grails动态查找器很多
下一篇: 孩子懒惰得到孩子ID