Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GRAILS, GORM custom mapping subclass in existing database.

 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic