当前位置 : 主页 > 手机开发 > 其它 >

继承 – GORM关系中的抽象类

来源:互联网 收集:自由互联 发布时间:2021-06-19
Grails GORM不会将抽象域类持久保存到数据库中,从而导致多态关系中断.例如: abstract class User { String email String password static constraints = { email(blank:false, nullable:false,email:true) password(blank:fals
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类中使用enum作为角色,因为我希望能够为不同的角色添加额外的属性,这在某些情况下可能没有意义(我不希望有单个用户,角色设置为RegularEmployee,以某种方式获取非null的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]
}

我不认为你会在当前的域映射中得到你想要的东西.

网友评论