*
The moose likes Groovy and the fly likes GRAILS, GORM custom mapping subclass in existing database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "GRAILS, GORM custom mapping subclass in existing database." Watch "GRAILS, GORM custom mapping subclass in existing database." New topic
Author

GRAILS, GORM custom mapping subclass in existing database.

Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Hi,

I've got MySQL database which is used for PHP CMS system and I would like to do Grails application based on that database. In MYSQL I have tables contact, individual and organisation
contact:
int id
String display_name
...

and
individual
int id
int contact_id ( FOREIGN KEY )
String first_name
String last_name

organisation
int id
int contact_od
Sring name

--
So I would like in Grails to have Contact as domain class and parent for Individual and Organisation
like


but I can't find a proper definition for mapping to get list of Individuals ( i didnt include my mappings in the code because they dont work ).

Can anybode help me with that ?
Dave Klein
author
Ranch Hand

Joined: Aug 29, 2007
Posts: 77
Did you get this figured out yet?

I haven't done this before so I can't say for sure how to map it, but you could try creating a similar model in a test app and let Grails create the schema and see what it does. That should give you some clues as to how to map it.

One thing you will need to do is set the mapping to table per class instead table per hierarchy. Like this:

static mapping = {
tablePerHierarchy false
}

Hope that helps,
Dave


Author of Grails: A Quick-Start Guide
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Hi Dave,

Finally I found time to play with this problem. I did not get what I wanted at the beginning, but it might be because of not-so-perfect database schema.
So My original tables are

If Individual extends Contact, then our id for individual is defined by 'contact_id'. There is no need of having normal id in this table, so I just dont map id.

So my classes definition are:


Anyway thanks ( at least you forced me to play with Grails )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: GRAILS, GORM custom mapping subclass in existing database.