Grails GORM does not persist abstract domain classes to the database, causing a break in polymorphic relationships. For example:
abstract class User {
String email
String password
static constraints = {
email(blank:false, nullable:false,email:true)
password(blank:false, password:true)
}
static hasMany = [membership:GroupMembership]
}
class RegularEmployee extends User {}
class Manager extends User {
Workgroup managedGroup
}
class Document {
String name
String description
int fileSize
String fileExtension
User owner
Date creationTime
Date lastModifiedTime
DocumentData myData
boolean isCheckedOut
enum Sensitivity {LOW,MEDIUM,HIGH}
def documentImportance = Sensitivity.LOW
static constraints = {
name(nullable:false, blank:false)
description(nullable:false, blank:false)
fileSize(nullable:false)
fileExtension(nullable:false)
owner(nullable:false)
myData(nullable:false)
}
}
causes
Caused by: org.hibernate.MappingException: An association from the table document refers to an unmapped class: User ... 25 more 2009-11-11 23:52:58,933 [main] ERROR mortbay.log - Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table document refers to an unmapped class: User: org.hibernate.MappingException: An association from the table document refers to an unmapped class: User
But in this scenario, I want the polymorphic effects of allowing any user to own a document, while forcing every user of the system to fit into one of the defined roles. Hence, User should not be directly instantiated and is made abstract.
I don't want to use an enum for roles in a non-abstract User class, because I want to be able to add extra properties to the different roles, which may not make sense in certain contexts (I don't wanna have a single User with role set to RegularEmployee that somehow gets a not null managedGroup).
Is this a bug in Grails? Am I missing something?