GORM关系中的抽象类 [英] Abstract Classes in GORM Relationships

查看:188
本文介绍了GORM关系中的抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre $ 抽象类用户{
字符串电子邮件
字符串密码
静态约束= {
email(空白:假,可空:false,电子邮件:真)
密码(空白:假,密码:真)
}

static hasMany = [会员:GroupMembership]
}

类RegularEmployee延伸用户{}

类经理扩展用户{
工作组managed_Group
}

类文档{
字符串名称
字符串描述
文件大小
字符串fileExtension
用户所有者
创建日期时间
日期lastModifiedTime
DocumentData myData
boolean isCheckedOut
enum灵敏度{LOW,MEDIUM,HIGH}
def documentImportance = Sensitivity.LOW

static constraints = {
名称(可空:假,空:假)
描述(可空:false,空:假)
fileSize(可空:false)
fileExtension(可为空):
所有者(可空值:false)
myData(可空值:false)
}
}

原因


引起:org.hibernate.MappingException:
表文档引用一个未映射的类:User
... 25更多2009-11-11 23:52:58,933 [main]错误mortbay.log - 嵌套在org.springframework.beans.factory .BeanCreationException:创建名为'messageSource'的bean时出错:bean
的初始化失败;嵌套的异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'transactionManager'的bean:在设置bean属性'sessionFactory'时无法将引用
解析为bean'sessionFactory';
嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'sessionFactory'的bean:调用init方法
失败;嵌套异常是org.hibernate.MappingException:表格文档中的
关联指的是一个未映射的类:User:
org.hibernate.MappingException:来自表文档
的关联指的是一个未映射的类:用户


但在这种情况下,我希望允许任何用户拥有文档的多态效应,同时强制每个用户该系统适合其中一个定义的角色。因此,用户不应该被直接实例化并被抽象化。



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



这是一个bug在Grails中?我错过了什么吗?

解决方案

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



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

 类角色{
//一些属性
static hasMany = [roleProperties:RoleProperty,...]
}
$ b $ class RoleProperty {
字符串名称
字符串值
静态belongsTo = [角色:作用]
}

我认为你不会尽管可以在当前的域映射中获得您想要的内容。


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?

解决方案

You might like to view the domain models for the Shiro, Nimble (uses Shiro), and/or Spring Security plugins. They create a concrete User domain and a concrete Role domain. Shiro in particular creates a UserRole domain for the many-to-many mapping.

Then on your Role domain, you can add whatever properties you want. If necessary, you can create a separate domain to allow for arbitrary properties like so:

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

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

I don't think you'll get what you're looking for in your current domain mapping though.

这篇关于GORM关系中的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆