Grails应用中的混合关系

发布于 2024-10-20 05:28:53 字数 201 浏览 2 评论 0原文

我有一个名为 Event 的域类。 活动报名人数较多,有现场有账号的用户,也有没有现场账号的用户。我希望注册用户能够跟踪他们的事件,因此事件-用户关系应该是双向的(用户由 Spring Security 创建)。仍然有一些注册与用户帐户无关。这很重要,因为活动是一种带有排名的现场比赛,活动结束时活动所有者会更新排名。如何实现这一目标?

I have a domain class which is called Event. Event has many registrations, which are users with account on site or people without it. I want registered user to be able to track their events, so Event-User relationship should be bidirectional (user is created by Spring Security). Still there are registrations which are not connected with user accounts. It's important since event is kind of a live competition with ranking, which is updated by event owner when event is over. How to achieve this?

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

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

发布评论

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

评论(1

悍妇囚夫 2024-10-27 05:28:53

这应该可行:

class User {
    static hasMany = [events:Event]
}


class Event {
    User user
    static constraints = {
       user(nullable:true)
    }
}

这样您可以从事件或用户的所有事件中获取用户。

编辑:

class User {
    static hasMany = [events:Event]
}


class Event {
    static hasMany = [users:User]
}

将用户添加到事件并

event.addToUsers(user)

从事件中删除用户并

event.removeFromUsers(user)

从事件中获取所有用户

event.users

我不太确定您如何处理未注册的用户。您根本没有关于该用户的信息吗(没有用户名等?)
我建议您在用户类“布尔注册”中添加一个标志。
因此,您也可以为未注册的用户创建一个 User 对象。

干杯马努

this should work:

class User {
    static hasMany = [events:Event]
}


class Event {
    User user
    static constraints = {
       user(nullable:true)
    }
}

this way you can get the user from the event or all events for the user.

EDIT:

class User {
    static hasMany = [events:Event]
}


class Event {
    static hasMany = [users:User]
}

add Users to the Event with

event.addToUsers(user)

remove Users from Event with

event.removeFromUsers(user)

get all Users from Event

event.users

I'm not quite sure how you handle the not registered Users. Do you have no information about this users at all (no username etc ?)
I would suggest you add a flag in the User class "Boolean registered".
So you can create a User object for not registered Users also.

cheers manu

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