Uploaded image for project: 'Grails'
  1. Grails
  2. GRAILS-6349

Many-to-Many relationships with composite ids fail


    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 1.3.1
    • Fix Version/s: 1.2.3, 1.3.2
    • Component/s: Persistence
    • Labels:
    • Environment:
      Mac OS X 10.6.3, Grails 1.3.1
    • Patch Submitted:
    • Testcase included:


      If you create two domain classes, both using composite ids and then try to establish a many-to-many relationship between the two, Grails will fail because it generates and invalid schema for the join table. Example:

      class M2MBook implements Serializable {
      Long isbn
      Long edition
      String title
      static belongsTo = M2MAuthor
      static hasMany = [authors:M2MAuthor]

      static mapping =

      { id composite:['isbn', 'edition'], generator:'assigned' }


      class M2MAuthor implements Serializable {
      Long family
      Long child
      String name
      static hasMany = [books:M2MBook]

      static mapping =

      { id composite:['family', 'child'], generator:'assigned' }


      Grails will fail upon startup when creating the database. The error is: "org.hibernate.MappingException: Foreign key (FK124F769E7A4CB7FA:m2mauthor_books [m2mbook_id])) must have same number of columns as the referenced primary key (m2mbook [isbn,edition])"

      The root problem is that GrailsDomainBuilder.bindDependentKeyValue doesn't properly identify that the composite key must be referenced and generates a mapping using simple id value. This is because shouldCollectionBindWithJoinColumn(property) returns false.

      I've already attached a test case and a patch. I've also verified that the Hibernate test suite passes given my fix.

      My fix is adding another clause to the if in GrailsDomainBuilder.bindDependentKeyValue that will handle the special case of a Many-to-Many with a composite key.

      1. grailsDomainBinder.patch
        0.7 kB
        Todd Miller
      2. ManyToManyCompositeIdTests.groovy
        3 kB
        Todd Miller


        Graeme Rocher added a comment - Fixed 1.3.x: http://github.com/grails/grails-core/commit/8540adff43fe942221f320999fea9f045d2e40ee Fixed 1.2.x: http://github.com/grails/grails-core/commit/26f2811edc62532b28dcd6d244727de93bf39b28 Thanks for the patch.


          • Assignee:
            Graeme Rocher
            Todd Miller
          • Votes:
            0 Vote for this issue
            0 Start watching this issue


            • Created: