Grails, GORM, MongoDB: discriminator column value is ignored

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Grails, GORM, MongoDB: discriminator column value is ignored

kostasg
I'm working on a Grails project, and I want to use GORM in order to read/write documents in a MongoDB. I have a specific scenario I want to implement, and I've been trying to make this work for 2-3 days now, but I can't find out what I'm doing wrong. I've read Grails documentation, many related posts on the web, but still nothing.

For the sake of simplicity I'm working on a simple enough scenario, which is the following:

I have 4 Domain classes:

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 = {}
}

Now, in a Service class, called CrudService, I have a method, e.g. foo(), and in that method I create a new Someone object, and save it to my Mongo database.

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

When I check my mongoDB collection to see what was saved, I see:

{
    "_id" : NumberLong(21),
    "animal" : {
        "_class" : "Dog",
        "dogName" : "Azor"
    },
    "name" : "A name",
    "version" : 0
}
So, as you can see, the document was saved, but the discriminator column name (and values) were completely ignored. The document was saved with the value "_class" as the column name (which is the default discriminator column name) and the Class name (in this case Dog) as its value.

Can anyone please help me and tell me what I'm doing wrong here?

Is there a chance that the discriminator feature of GORM, works correctly for RDBMS but not for NoSQL databases?

Thank you in advance for any suggestion.