title: Grails GORM: How do I create a composite primary key and use it for a table relationship?
body: <p>I have two tables, one of which (legacy table: A) has two fields that should serve as a composite foreign key and the other one (new table: B) should use a composite primary key for a <code>each row:A has one row:B</code> relationship. How do I describe these tables in terms of GORM?</p>
  
  <p>So far I've been able to create a domain class that reflects the legacy table:A</p>
  
  <pre><code>class A {
  
      ...
      //composite foreign key to link B class
      String className;
      String eventName;
  
      B b; //instance of B to be related
  
      static mapping = {
          table 'a_table';            
          id column: 'id';
          className column: 'class_name';
          eventName column: 'event_name';
          //b: ???
      }
  }
  </code></pre>
  
  <p>which works, but I can't create a <code>new class:B</code> and the relationship.</p>
  
  <p>I tried to declare B as:</p>
  
  <pre><code>class B implements Serializable{
  
      static auditable = true;
  
      String name;
      String className;
      String eventName;
  
      static mapping = {
          //supposed to make a composite PK
          id composite:[className, eventName] 
      }
  }
  </code></pre>
  
  <p>but this won't compile with a<br>
  <code>ERROR context.GrailsContextLoader  - Error executing bootstraps: Error evaluating ORM mappings block for domain [com.package.B]:  No such property: eventName for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder</code></p>
  
  <p>What I want is something like:</p>
  
  <pre><code>static mapping = {
      ...
      b composite: [b.className:className, b.eventName:eventName]
      //or whatever is the right way for this to be done.
  }
  </code></pre>
  
  <p>for the A class to make GORM handle this relation.</p>