Grails,GORM,MongoDB:忽略了discriminator列值

时间:2015-01-04 09:12:45

标签: mongodb grails groovy gorm discriminator

我正在开发Grails项目,我想使用GORM来读取/写入MongoDB中的文档。我有一个我想要实现的特定场景,我一直在努力让这项工作工作2-3天,但我无法找出我做错了什么。我已经阅读了Grails文档,网上有很多相关的帖子,但仍然没有。

为了简单起见,我正在研究一个简单的场景,如下所示:

我有4个Domain类:

Someone.groovy

class Someone {

    static mapWith = 'mongo'

    static embedded = ['animal']

    static constraints = {}

    String name

    Animal animal
}

Animal.groovy

class Animal {

    static mapWith = 'mongo'

    static mapping = {
        discriminator column: "type"
    }

    static belongsTo = Someone

    static constraints = {}
}

Dog.groovy

class Dog extends Animal {

    static mapWith = 'mongo'

    String dogName

    static mapping = {
        discriminator value: "dog"
    }

    static belongsTo = Someone

    static constraints = {}
}

Cat.groovy

class Cat extends Animal {

    static mapWith = 'mongo'

    String catName

    static mapping = {
        discriminator value: "cat"
    }

    static belongsTo = Someone

    static constraints = {}
}

现在,在名为 CrudService 的服务类中,我有一个方法,例如 foo() ,在该方法中,我创建了一个新的 Someone 对象,并将其保存到我的Mongo数据库中。

Someone someone = new Someone(name: "A name", animal: new Dog(dogName: "Azor"))
someone.save(flush: true)

当我检查我的mongoDB集合以查看保存的内容时,我看到:

{ 
    "_id" : NumberLong(21), 
    "animal" : { 
        "_class" : "Dog", 
        "dogName" : "Azor" 
    }, 
    "name" : "A name", 
    "version" : 0 
}

因此,正如您所看到的,文档已保存,但是鉴别器列名称(和值)完全被忽略。文档保存时使用值 "_class" 作为列名称(默认标识符列名称)和类名称(在本例中为 Dog )as它的价值。

任何人都可以帮助我,告诉我这里我做错了什么吗?

GORM的鉴别器功能是否有可能适用于RDBMS而不适用于NoSQL数据库?

(目前我也在看Morphia以了解它是否可以解决我的问题,但我更喜欢使用GORM,因为它是Grails的默认ORM。)

提前感谢您的任何建议。

0 个答案:

没有答案