Back Forum Reply New

Composite Ids problem

I am having trouble implementing the following scenario using Grails.  I would like to use the joinTable for a 1-many relationship between 2 entities to create a separate mapping table.  Each of the 2 entities have composite ids - the complication comes from this.  I would like the mapping table to be a separate table containing the composite id foreign key columns for each entity.  Is it possible to implement this using Grailsl?  Thanks in advance.

This works for me using a somewhat contrived example:Code:
class Book {  String title  Date pubDate
  static mapping = {     id composite: ['title', 'pubDate']  }
}

Code:
class Author {  String firstName  String lastName  static hasMany = [books: Book]
  static mapping = {     id composite: ['firstName', 'lastName']  }
}
which creates this DDL:

Code:
create table author (first_name varchar(255) not null, last_name varchar(255) not null, version bigint not null, primary key (first_name, last_name));
create table author_book (author_first_name varchar(255), author_last_name varchar(255), book_title varchar(255), book_pub_date timestamp);
create table book (title varchar(255) not null, pub_date timestamp not null, version bigint not null, primary key (title, pub_date));
alter table author_book add constraint FK2A7A111D3D4854D1 foreign key (book_title, book_pub_date) references book;
alter table author_book add constraint FK2A7A111D30B4B479 foreign key (author_first_name, author_last_name) references author;
Use 'grails schema-export' and look at the generated target/ddl.sql when testing with your code.

Thanks!  This is exactly what I was looking for.  I was trying to use 'joinTable' and was getting all sorts of errors.  The approach shown here should work for what I am trying to do.  Thanks again!
¥
Back Forum Reply New