apache / apache/grails-core

Documentation appears to be incorrect for joinTable & column in many-to-many mapping

Open
#14,625 1 comment 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

The documentation at http://gorm.grails.org/latest/hibernate/manual/index.html#_many_to_many_mapping shows how to change the column name & join table for a many-to-many relationship by creating a mapping in both classes:
```groovy
class Group {
...
static mapping = {
people column: 'Group_Person_Id',
joinTable: 'PERSON_GROUP_ASSOCIATIONS'
}
}
class Person {
...
static mapping = {
groups column: 'Group_Group_Id',
joinTable: 'PERSON_GROUP_ASSOCIATIONS'
}
}
```

This documentation seems to show that the `column` should be the ID of the other class, but it appears that it is the ID of the current class. I created a test to demonstrate this.

```groovy
class A {
Long id
String value

static hasMany = [
b: B,
]
static mapping = {
id generator: 'assigned'
b column: 'B_ID', joinTable: 'A_B'
}

static def allJoins() {
def result
withSession { session ->
def q = session.createSQLQuery("SELECT A_ID, B_ID FROM A_B")
result = q.list()
}
return result
}
}
```

```groovy
class B {
Long id
String value

static belongsTo = A
static hasMany = [
a: A,
]

static mapping = {
id generator: 'assigned'
a column: 'A_ID', joinTable: 'A_B'
}
}
```

```groovy
import grails.test.mixin.integration.Integration
import grails.transaction.Rollback
import spock.lang.Specification

@Integration
@Rollback
class ABSpec extends Specification {
void "ab join columns"() {
given:
def a1 = new A(value: 'A1')
a1.id = 101
a1.save()
def b1 = new B(value: 'B1')
b1.id = 201
b1.save()
a1.addToB(b1)
a1.save(flush: true)

expect:
A.allJoins() == [[101, 201]]
}
}
```

This test fails with the IDs in the incorrect order (the result is `[[201,101]]`)

Contributor guide

Open the contributing guide

Research direction

Start with the many-to-many mapping section at the linked GORM Hibernate manual URL and compare its column and joinTable example with the A and B mappings in the issue. Run the supplied ABSpec integration test to confirm the documented column order, then update the documentation so the example matches the observed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
databases, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.