inheritance - GORM 关系中的抽象类

标签 inheritance grails abstract-class orm grails-orm

Grails GORM 不会将抽象域类持久化到数据库,从而导致多态关系中断。例如:

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)
    }
}

原因

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



但在这种情况下,我想要允许任何用户拥有文档的多态效果,同时强制系统的每个用户都适合定义的角色之一。因此, User 不应直接实例化,而是抽象的。

我不想对非抽象 User 类中的角色使用枚举,因为我希望能够为不同的角色添加额外的属性,这在某些情况下可能没有意义(我不想有一个角色设置为 RegularEmployee 的单个用户以某种方式获得了一个非空的 managedGroup)。

这是 Grails 中的错误吗?我错过了什么吗?

最佳答案

您可能希望查看 Shiro、Nimble(使用 Shiro)和/或 Spring Security 插件的域模型。他们创建了一个具体的用户域和一个具体的角色域。 Shiro 特别为多对多映射创建了一个 UserRole 域。

然后在您的角色域上,您可以添加任何您想要的属性。如有必要,您可以创建一个单独的域以允许任意属性,如下所示:

class Role {
    //some properties
    static hasMany = [roleProperties:RoleProperty, ...]
}

class RoleProperty {
    String name
    String value
    static belongsTo = [role:Role]
}

不过,我认为您不会在当前的域映射中获得所需的内容。

关于inheritance - GORM 关系中的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1720533/

相关文章:

java - 将方法委托(delegate)给Java中的子类

grails - 即使路径正确也无法使用Grails

spring - GORMless Grails应用程序无法查找丢失的(和不必要的)transactionManager

c# - 为什么要用它继承的抽象类而不是它自己的类来声明一个对象

c++ - Clang -Wweak-vtables 和纯抽象类

c++ ctor 和 dtor 的工作方式不同

javascript - 从另一个实例更新实例的属性

Django 模板 - 显示 {% block %} 标记之前的 if 语句无法正常工作?

java - 具有元素数量限制的 Groovy .collect

c# - 如何在C#中使用抽象类