Sunday 15 January 2012

Relationships between Grails domain classes with inheritance -



Relationships between Grails domain classes with inheritance -

i have next class. in src/groovy,

class profile { string firstname string middlename string lastname byte[] photo string bio }

the domain classes basicprofile , academicprofile extend profile.

class basicprofile extends profile { user user date datecreated date lastupdated static constraints = { firstname blank: false middlename nullable: true lastname blank: false photo nullable: true, maxsize: 2 * 1024**2 bio nullable: true, maxsize: 500 } static mapping = { tablepersubclass true } } class academicprofile extends profile { user user string dblpid string scholarid string website date datecreated date lastupdated static hasmany = [publications: publication] static constraints = { importfrom basicprofile dblpid nullable: true scholarid nullable: true website nullable: true, url: true publications nullable: true } static mapping = { tablepersubclass true } }

then there publication class.

class publication { string dblpid string scholarid string title string description date publicationdate int citations date datecreated date lastupdated static belongsto = [academicprofile] static hasone = [publisher: publisher] static hasmany = [academicprofiles: academicprofile] static constraints = { dblpid nullable: true scholarid nullable: true title blank: false, maxsize: 100 description nullable: true, maxsize: 500 publicationdate: nullable: true academicprofiles nullable: false } }

finally, have user class.

class user { string username string password string email date datecreated date lastupdated static hasone = [basicprofile: basicprofile, academicprofile: academicprofile] static constraints = { username size: 3..20, unique: true, nullable: false, validator: { _username -> _username.tolowercase() == _username } password size: 6..100, nullable: false, validator: { _password, user -> _password != user.username } email email: true, blank: false basicprofile nullable: true academicprofile nullable: true } }

my questions follows.

i want relationship each user may optionally have profile (either basicprofile or academicprofile). tried static hasone = [profile: profile] got errors saying profile not agree hasone relationship. current setup have workaround. there no way user can have 1 profile basicprofile or academicprofile? secondly, in current setup, error: invocation of init method failed; nested exception org.hibernate.mappingexception: association table academic_profile_publications refers unmapped class: org.academic.academicprofile when seek run it. google search tells me problem classes inheriting other classes. technically, if don't have hasmany relationship in publication academicprofile, should work without issues. don't want that. because publication has many authors (academicprofiles in case) , author may have many publications. there way prepare this?

you're not using hibernate inheritance - requires of classes mapped. you're using regular java/groovy inheritance inherit properties , methods base of operations classes. hibernate isn't aware of that, can't queries on unmapped base of operations class.

i'm not sure why it's complaining academicprofile, secondary bug caused core issue.

i find hibernate inheritance way frustrating utilize in cases, utilize approach when there shared code.

it should work if move profile grails-app/domain. 1 time should move tablepersubclass mapping config base of operations class , specify once.

grails inheritance gorm

No comments:

Post a Comment