简体   繁体   English

两个类之间的一对多和多对多关系? (Grails ORM)

[英]One-to-many and many-to-many relationship between two classes? (Grails ORM)

I'm having an issue dealing with two entities which should have a relationship of one to many and many to many. 我在处理两个实体之间存在问题,两个实体应该具有一对多和多对多的关系。 Let me show you the case: there are users, which create and take part of activities. 让我向您展示这种情况:有些用户创建并参与了活动。 So an activity could have several users and just one activity creator, meanwhile an user can create and belong to many activities. 因此,一个活动可以有多个用户,只有一个活动创建者,而一个用户可以创建并属于许多活动。

So I did something like this: 所以我做了这样的事情:

class User {

    static hasMany = [activities:Activity, activitiesCreated: Activity]
    static mappedBy = [activitiesCreated: "creator"]
...
}

class Activity{

    static hasMany = [users:User]
    static belongsTo = [users:User]
    Usuario creator
...
}

This raises a runtime exception, which is this one: No owner defined between domain classes [class User] and [class Activity] in a many-to-many relationship. 这将引发一个运行时异常,即:在多对多关系中的域类[类User]和[类Activity]之间未定义所有者。 Example: static belongsTo = Activity 示例:静态belongsTo =活动

The many-to-many relationship works fine if I don't try to implement the one-to-many, so it wouldn't be the problem. 如果我不尝试实现一对多关系,则多对多关系会很好,所以这不是问题。

And this is where I'm stuck :/ 这就是我被困的地方:/

I would have third entity to realize the many to many relationship. 我将拥有第三个实体来实现多对多关系。 For example, let say represent actual execution of the Activities as an Event, which means a Event has one ore more Activities, time stamp/time frame, and one ore more participating Users. 例如,假设将活动的实际执行表示为一个事件,这意味着一个事件具有一个或多个活动,时间戳/时间范围和一个或多个参与用户。 A user can create one ore more activity and by the same token the owners of Activity will be considered to own the Event. 用户可以创建一个或多个活动,并且通过相同的令牌,活动的所有者将被视为拥有该活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM