简体   繁体   English

Grails 关系 m:n:n

[英]Grails Relation m:n:n

We work with an existing mysql database under grails which contains am:n relation.我们在 grails 下使用现有的 mysql 数据库,其中包含 am:n 关系。 No problem here.这里没问题。 But now we are in the situation that i have to add a second n relation entry, which links to the same table and has to be associated to the first n entry.但是现在我们的情况是我必须添加第二个 n 关系条目,该条目链接到同一个表并且必须与第一个 n 条目相关联。

If it were for the database, I would simply create a table which looks like:如果是用于数据库,我会简单地创建一个如下所示的表:

field m_id -> links to m table
field n_id1 -> links to n table
field n_id2 -> links to n table

But how can this be represented in a grails domain class?但是如何在 grails 域类中表示它呢?

Possibly the answer can already be found somewhere, but the searches I did were not successful, maybe due to lack of search term creativity.可能答案已经在某处找到了,但我所做的搜索没有成功,可能是由于缺乏搜索词的创造力。

EDIT:编辑:

Trying to clarify the question: we have a many-to-many relation, but with two items on one side, which have to maintain association to each other (and it also must be clear which is for example the original and which is the replacement item), so they can not be seperated into two separate entries into the relation.试图澄清这个问题:我们有一个多对多的关系,但是在一侧有两个项目,它们必须保持相互关联(并且还必须清楚哪个是原始的,哪个是替代的item),所以它们不能被分成两个单独的关系条目。

Hmm... try to think of racing car drivers nominating for a series of races, and every nomination has to contain the driver and his substitute.嗯...试着想想赛车手提名一系列比赛,每个提名都必须包含车手和他的替补。 Races would be m (left hand), driver would be n1 and substitute would be n2.比赛为 m(左手),车手为 n1,替补为 n2。 (It was really hard work to find an example...) (找个例子真的很辛苦……)

EDIT:编辑:

By coincidence I found this question which addresses the same problem but also left off rather unsolved.巧合的是,我发现这个问题解决了同样的问题,但也没有解决。

Take a look at the documentation, is pretty clear: http://grails.github.io/grails-doc/2.5.0/guide/GORM.html#gormAssociation看看文档,很清楚: http : //grails.github.io/grails-doc/2.5.0/guide/GORM.html#gormAssociation

Anyway I'm not sure I understand what you're trying to do, but let's try.无论如何,我不确定我是否理解您要做什么,但让我们尝试一下。

If it's a one-to-one relation, you could simply do something like this:如果是一对一的关系,你可以简单地做这样的事情:

class DomainA {

    DomainB domainB
    DomainC firstDomainC
    DomainC secondDomainC
}

class DomainB {
}

class DomainC {
}

That would create the following fields in the "domain_a" table:这将在“domain_a”表中创建以下字段:

  • "domain_b_id" “domain_b_id”
  • "first_domain_c_id" “first_domain_c_id”
  • "second_domain_c_id" “second_domain_c_id”

If it's a one-to-many relationship from DomainA to both DomainB and DomainC with two differentiated collections of DomainC in DomainA, you must have two different DomainA properties in DomainC to be able to map it.如果是从 DomainA 到 DomainB 和 DomainC 的一对多关系,并且在 DomainA 中有两个不同的 DomainC 集合,则必须在 DomainC 中有两个不同的 DomainA 属性才能映射它。

The example with Airport and Flight in the documentation:文档中机场和航班的示例:

class Airport {
    static hasMany = [outboundFlights: Flight, inboundFlights: Flight]
    static mappedBy = [outboundFlights: "departureAirport",
                   inboundFlights: "destinationAirport"]
}
class Flight {
   Airport departureAirport
   Airport destinationAirport
}

The flight needs to Airport properties in order to be able to distinguish which one is mapping the right hasMany collection in Airport.飞行需要机场属性,以便能够区分哪个映射正确的hasMany集合在机场。

EDIT: for the race-driver example编辑:对于赛车手示例

Grails supports many-to-many relations but one of the ends must be the principal one and the other must additionally have a belongsTo to it. Grails 支持多对多关系,但其中一个目的必须是主体,另一个必须另外有一个属于它的对象。 Though this is not the case, since neither race belongs to driver nor driver belongs to race.但事实并非如此,因为比赛既不属于车手,也不属于车手。 I would use relation with a property: "drives" with a property "mainDriver".我会使用与属性的关系:带有属性“mainDriver”的“驱动器”。 That cannot be mapped directly, you need to use a domain for the relation:不能直接映射,您需要为关系使用域:

class Race {
    static hasMany = [ participants: DriverRace ]

    def mainDrivers() {
        getDrivers( false )
    }

    def substitutes() {
        getDrivers( false )
    }

    private getDrivers( mainDriver ) {
        DriverRace.withCriteria {
            eq( "race", this )
            eq( "mainDriver", mainDriver )
        }.collect { it.driver }
    }
}

class Driver {
    static hasMany = [ races: DriverRace ]
}

class DriverRace {
    static belongsTo = [ race: Race, driver: Driver ]

    boolean mainDriver

}

We thought we were able to solve the problem by inserting a second reference to domain/table n in the mapping property of (left hand side) domain m.我们认为我们能够通过在(左侧)域 m 的映射属性中插入对域/表 n 的第二个引用来解决问题。 grails then seems to put a second reference to n on the right hand side of the relation.然后 grails 似乎在关系的右侧放置了对 n 的第二个引用。 But that turned out to be a hopeful weekend dream.但结果证明这是一个充满希望的周末梦想。

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

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