父子懒获取子id
我有一个父子关系,其中父级有许多子级,而子级属于父级。获取模式是默认的(惰性)。当我获取父级时,sql 相当于“select * from child whereparent_id = ?”也被执行 - 也就是说,它获取所有相关子项的每个字段。 (这是懒惰的行为吗?)我需要抑制对子项的所有字段的查询,因为其中有一些我不想获取的 clob,除非我明确通过 id 获取子项。我怎样才能实现这个效果?我是否必须创建另一个没有 clob 属性的“摘要”域子类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您已经解决了延迟加载问题,所以我不会对此发表评论。
我处理仅从子项中获取某些数据的问题的方法是编写一个自定义 hql 查询,该查询仅返回您感兴趣的值。因此,如果您只需要子项的 id,请执行某种查询喜欢
参见 http://grails.org/doc/latest/ref/Domain %20Classes/executeQuery.html。
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
See http://grails.org/doc/latest/ref/Domain%20Classes/executeQuery.html.
您是否知道它会同时获得所有这些内容?我问这个问题是因为,一旦您开始使用调试器、println、日志记录来查看数据,以便向您显示内容,Grails 会认为您已经请求了该数据,并将去获取它。所以它可能是延迟加载的,但是当您试图验证它没有被获取时,您告诉 Grails 去获取它。
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.
啊但是,我明白了。 “show”的默认视图是访问子属性。
现在,我在渲染为 XML 或 JSON 时也看到了相同的行为,但这可能是一个类似的问题 - 子属性正在被询问?
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?