Grails 多对一公开 id 而无需延迟加载?
Grails - 如何在不强制加载整个对象的情况下公开多对一关联属性的 id?
class Task {
User belongsTo
User createdBy
}
class User {
...
}
我可以从任务中获取用户 ID,而不会导致整个用户对象的延迟加载吗?有没有一种方法可以做到这一点,或者它会涉及每个类的自定义查询?这实际上涉及通过 BlazeDS 返回的 DTO 对象 - 我需要的只是 id,而不一定是关联的对象。
Grails - How can I expose the id of a many-to-one association property without forcing a load of the entire object?
class Task {
User belongsTo
User createdBy
}
class User {
...
}
Can I get get the User ids from Task without causing lazy loading of the entire User objects? Is there a way to do this in general, or will it involve a custom query for each class? This actually involves DTO objects to return via BlazeDS - all I need are the ids, not the associated objects necessarily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,Grails 启用了惰性关联,因此您不必担心所有链接对象的加载,除非您启用急切获取。此外,您可以使用命名查询或休眠标准投影:
例如,
上面的代码只会提取名为 Joe 的用户的任务 ID。
Grails by default has lazy association enabled so you don't have to worry about the loading of all chained objects unless you enable eager fetching. further more you could use named queries or hibernate criteria projections:
e.g.
the above will pull only the id of a task for a user called Joe.
显然你需要使用魔术符号belongsToId和createdById - 请参阅GRAILS-2570
Apparently you need to use the magic notation belongsToId and createdById - see GRAILS-2570