Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
abryzakez opened this issue Feb 24, 2017 · 1 comment

Comments

@abryzakez
Copy link

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:

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.

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
    }
}
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'
    }
}
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]])

@snimavat
Copy link
Contributor

@abryzakez can u try removing the column: 'A_ID', column: 'B_ID' and keep just join table, and see what happens..

jamesfredley referenced this issue in apache/grails-data-mapping Mar 13, 2025
@jdaugherty jdaugherty transferred this issue from apache/grails-data-mapping Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants