Grails 跨同一个表的多对多关系

发布于 2024-10-26 05:10:37 字数 350 浏览 7 评论 0原文

我正在使用 Grails,并且想要建立单向的多对多关系。

在我的应用程序中,员工可以为另一名员工“添加书签”,以便快速访问他们并留下笔记。员工不需要知道谁给他们添加了书签。

换句话说,我本质上希望拥有一个可以用于此目的的 employee.bookmarks 属性。

到目前为止,我已经找到了 有关 Grails ORM 的多对多关系的文档,但这似乎只跨两个不同表。

I'm using Grails and I want to have a unidirectional many-to-many relationship.

In my application, an Employee can "bookmark" another Employee in order to quickly access them to leave notes. Employees need not know who has bookmarked them.

In other words, I essentially want to have an employee.bookmarks property that I can use for this.

Thus far, I've found the documentation on many-to-many relationships for Grails ORM, but this seems to be exclusively across two different tables.

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

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

发布评论

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

评论(2

巷雨优美回忆 2024-11-02 05:10:38

听起来你只需要一个常规的单向一对多:

class Employee {
   ...
   static hasMany = [bookmarks: Employee]
}

Sounds like you just need a regular unidirectional 1-many:

class Employee {
   ...
   static hasMany = [bookmarks: Employee]
}
冰之心 2024-11-02 05:10:38

您可以使用瞬态属性和一个附加表“书签”来完成此任务:

class Employee {

    String name
    static transients = ["bookmarks"]

    def getBookmarks() {
        Bookmark.findAllByBookmarker(this, [sort: "id", order: "asc"])
    }
    ...
}

class Bookmark implements Serializable {

  Employee bookmarker // the employee who bookmark someone
  Employee bookmarkee // the employee who is bookmarked

  static mapping = {
    table "Bookmark"
    id composite: ['bookmarker', 'bookmarkee']
    bookmarker(column: "BOOKMARKER_ID")
    bookmarkee(column: "BOOKMARKEE_ID")
    version false
  }

    static Bookmarker get(long bookmarkerId, long bookmarkeeId) {
        find 'from Bookmark where bookmarker.id=:bookmarkerId and bookmarkee.id=:bookmarkeeId',
            [bookmarkerId: bookmarkerId, bookmarkeeId: bookmarkeeId]
    }
    ...
}

该方法使用表“书签”来存储员工之间的关系,因此可以有2个人为同一员工添加书签。请注意,Bookmark 类必须实现 Serialized。

You can use transient properties and an additional table "Bookmark" to do this task:

class Employee {

    String name
    static transients = ["bookmarks"]

    def getBookmarks() {
        Bookmark.findAllByBookmarker(this, [sort: "id", order: "asc"])
    }
    ...
}

class Bookmark implements Serializable {

  Employee bookmarker // the employee who bookmark someone
  Employee bookmarkee // the employee who is bookmarked

  static mapping = {
    table "Bookmark"
    id composite: ['bookmarker', 'bookmarkee']
    bookmarker(column: "BOOKMARKER_ID")
    bookmarkee(column: "BOOKMARKEE_ID")
    version false
  }

    static Bookmarker get(long bookmarkerId, long bookmarkeeId) {
        find 'from Bookmark where bookmarker.id=:bookmarkerId and bookmarkee.id=:bookmarkeeId',
            [bookmarkerId: bookmarkerId, bookmarkeeId: bookmarkeeId]
    }
    ...
}

This method uses table "Bookmark" to store the relations between employees, so it is possible to have 2 people bookmark the same employee. Note that class Bookmark must implements Serializable.

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