database - Grails Relation m:n:n -
we work existing mysql database under grails contains m:n relation. no problem here. in situation have add second n relation entry, links same table , has associated first n entry.
if database, create table looks like:
field m_id -> links m table field n_id1 -> links n table field n_id2 -> links n table
but how can represented in grails domain class?
possibly answer can found somewhere, searches did not successful, maybe due lack of search term creativity.
edit:
trying clarify question: have many-to-many relation, 2 items on 1 side, have maintain association each other (and must clear example original , replacement item), can not seperated 2 separate entries relation.
hmm... try think of racing car drivers nominating series of races, , every nomination has contain driver , substitute. races m (left hand), driver n1 , substitute n2. (it hard work find example...)
edit:
by coincidence found this question addresses same problem left off rather unsolved.
take @ documentation, pretty clear: http://grails.github.io/grails-doc/2.5.0/guide/gorm.html#gormassociation
anyway i'm not sure understand you're trying do, let's try.
if it's one-to-one relation, this:
class domaina { domainb domainb domainc firstdomainc domainc seconddomainc } class domainb { } class domainc { }
that create following fields in "domain_a" table:
- "domain_b_id"
- "first_domain_c_id"
- "second_domain_c_id"
if it's one-to-many relationship domaina both domainb , domainc 2 differentiated collections of domainc in domaina, must have 2 different domaina properties in domainc able map it.
the example airport , flight in documentation:
class airport { static hasmany = [outboundflights: flight, inboundflights: flight] static mappedby = [outboundflights: "departureairport", inboundflights: "destinationairport"] } class flight { airport departureairport airport destinationairport }
the flight needs airport properties in order able distinguish 1 mapping right hasmany collection in airport.
edit: race-driver example
grails supports many-to-many relations 1 of ends must principal 1 , other must additionally have belongsto it. though not case, since neither race belongs driver nor driver belongs race. use relation property: "drives" property "maindriver". cannot mapped directly, need use domain relation:
class race { static hasmany = [ participants: driverrace ] def maindrivers() { getdrivers( false ) } def substitutes() { getdrivers( false ) } private getdrivers( maindriver ) { driverrace.withcriteria { eq( "race", ) eq( "maindriver", maindriver ) }.collect { it.driver } } } class driver { static hasmany = [ races: driverrace ] } class driverrace { static belongsto = [ race: race, driver: driver ] boolean maindriver }
Comments
Post a Comment