gorm - inheritance in Grails domain model -
my grails app's domain model has following requirements:
- a user belong 0 or 1 organisations
- an organisation either charity or company
- charities , companies have some common fields , (non-nullable) fields unique each organisation type
i put common organisation fields abstract organisation
class charity
, company
both extend. can't store hierarchy in single table because there non-nullable fields specific each organisation type. relevant parts of domain model shown below:
class user { string name static belongsto = [organization: organization] static constraints = { organization nullable: true } } abstract class organization { string name static hasmany = [users: user] static mapping = { tableperhierarchy false } } class charity extends organization { // charity-specific fields go here } class company extends organization { // company-specific fields go here }
when @ mysql schema generated model, inheritance relationship between organisation-company , organisation-charity seems have been ignored. although there organisation table name column, has no primary-foreign key relationship either company or charity
- i see same result ianroberts both mysql , h2. in other words: no join table generated, expected
organization_id
fk inusers
table. - with "table per subclass" mapping (
tableperhierarchy false
), end implied one-to-one relationship in database. primary keyscharity
,company
have same value pk parentorganization
. schema generated gorm/hibernate3 doesn't appear enforce referential integrity constraints. it's pure hibernate magic. bit more detail here
Comments
Post a Comment