Thursday, May 5, 2011

Modeling many-to-many relationship in Grail on top of a legacy database

Hi

I have a simple ticket logging application build on LAMP.

I am currently playing around with grails. I want to build a demo app that uses the existing MySql database without changing the database too much.

There is a many-to-many relationship in the database: 'client' table is mapped to the 'user' table through the 'cliet_contact' table (i.e. not the standard 'client_user' convention).

How would I translate this into grails domain classes using grail 1.1?

Any help would be appreciated.

Thanks!

From stackoverflow
  • You can use the joinTable keyword in your mapping to specify the table name. Here's the example from that page:

    class Book {
        String title 
        static belongsTo = Author 
        static hasMany = [authors:Author]
        static mapping = { 
            authors joinTable:[name:"mm_author_books", key:'mm_book_id' ] 
        }
    }
    
    class Author { 
        String name
        static hasMany = [books:Book]
        static mapping = { 
            books joinTable:[name:"mm_author_books", key:'mm_author_id'] 
        }
    }
    
    Heinrich Filter : Thanks Ben That is precisely what I needed!

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.