以 Map 形式检索域对象实例

发布于 2024-09-06 22:12:50 字数 102 浏览 1 评论 0原文

我想知道是否有一种方法可以将域实例检索为 Map,其中键是域对象的 id。

更具体地说,我想做 myDomainObject.list() 来返回 Map 而不是 List。

I wonder if there is a way to retreive domain instances as a Map where the key is the id of the domain object.

more specific i would like to do myDomainObject.list() to return a Map instead of a List.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2024-09-13 22:12:50

您可以在域类中轻松创建此方法,例如:

class Person {

   String name

   static Map<Long, Person> mapAll() {
      def map = [:]
      list().each { map[it.id] = it }
      map      
   }
}

You can create this method easily in your domain class, for example:

class Person {

   String name

   static Map<Long, Person> mapAll() {
      def map = [:]
      list().each { map[it.id] = it }
      map      
   }
}
定格我的天空 2024-09-13 22:12:50

如果这不是您唯一需要它的地方,您还可以使用 Burts 代码通过 ExpandoMetaClass

这将集中功能并保持域类的干净。

If this isn't the only place you need it, you could also use Burts code to extend eiter domain classes or the map itself via the ExpandoMetaClass.

This would centralize the functionality and keep your domain classes clean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文