Announcement Announcement Module
Collapse
No announcement yet.
Grails/GORM: How can I create a composite primary key with two foreign keys? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Grails/GORM: How can I create a composite primary key with two foreign keys?

    I'm hoping someone can shed some light on how to declare the "BookShelf" class I'm trying to create in Grails/GORM. I have the following simple domain objects:


    Code:
    class Book {
         int      bookID
         String title
         String author
    
         // static mapping assigns primary key to bookID
    }
    Code:
    class Shelf {
        int shelfID
        String name
    
       // static mapping assigns primary key to shelfID
    }
    What I want to do is create a "BookShelf" domain where I can set the number of book copies on a particular book shelf, such as follows:

    Code:
    class BookShelf {
        int bookID    // linked to "Book.bookID"
        int shelfID    // linked to "Shelf.shelfID"
        int bookCopies
    
        // bookID and shelfID would be a composite unique primary key constrained by an existing bookID and shelfID
    }

    I'd like the BookShelf primary key to be a unique composite of Book.bookID and Shelf.shelfID -- in order to be able to create a Shelf that has multiple copies of a "Book".

    I'd appreciate any help anyone could give on this, or even a different way to approach this relationship in Grails.
Working...
X