jkuehn / gorm-mongodb

Grails GORM implementation for the MongoDB document oriented database
http://www.grails.org/plugin/mongodb-morphia
Apache License 2.0
21 stars 14 forks source link

Extending Classes raised MultipleId complaint #1

Closed alexduan closed 13 years ago

alexduan commented 13 years ago

Environment: Grails 1.3.6, mongodb-morphia-0.6

I have an abstract class, BaseDomain:


abstract class BaseDomain {
    Date dateCreated
    Date lastUpdated
}

Which is extended by Person:


class Person extends BaseDomain {
    String firstName
    String lastName
}

When starting up my Grails app, I get the following error:


adding domain class com.redshoe.live.domain.entity.Person to morphia
2011-02-23 23:02:35,891 [main] ERROR plugins.DefaultGrailsPlugin  - Error processing mongodb domain Artefact > com.redshoe.live.domain.entity.Person: Number of violations: 2
MultipleId complained about com.redshoe.live.domain.entity.Person. : More than one @Id Field found (_id, _id).DuplicatedAttributeNames complained about com.redshoe.live.domain.entity.Person.id : Mapping to MongoDB field name '_id' is duplicated; you cannot map different java fields to the same MongoDB field.

This error only occurs if the superclass also resides within the grails-app/mongo source folder (I tried moving the abstract class out just to check). Unfortunately, that is not a feasible solution for all my classes since I have another class extending Person, and I get the same error.

alexduan commented 13 years ago

This issue should be closed with release 0.6.1 and 0.6.2

countvajhula commented 12 years ago

Hello, I am seeing this issue in my app, and I am using version 0.7.3.

I think it might have something to do with the plugin automatically inserting an ObjectId id field if you don't manually specify an id field. As a result, the parent object gets an automatic ObjectId id and the same happens for the child class, and this results in the duplicates. But I'm not sure.

To give you a full picture of what I am doing: I have 6-7 classes created in /mongo via create-mongodb-class. One of these is the base class MongoObject, which contains many fields that are common to all other classes. 4-5 other classes extend MongoObject. There is another class called ParentClassA which extends MongoObject, and which further has two children of its own, ClassB and ClassC.

If I try to run my grails app with this setup, I get complaints like:

  1. "EmbeddedAndId": Embedded classes cannot have an _id -- this is because one of the classes in /mongo is actually an embedded class, and it extends the base class MongoObject which apparently automatically gets an ObjectId field even if I don't specify one.
  2. " field name '_id' is duplicated; you cannot map different java fields to the same MongoDB field." -- if classes in /mongo where an @Id field is not specified are automatically being assigned an ObjectId by the gorm-mongodb plugin (as I suspect), then this is probably a result of both MongoObject and any child class each receiving an ObjectId.

Attempted workaround: If I move the class MongoObject into a different folder (src/groovy) as indicated in one of the above comments, and leave out the @Entity annotation, then the above errors go away.

BUT: I'm not sure if this works yet, since I am unable to get the @AlsoLoad annotation working in this (or any) configuration. It's probably unrelated to this issue, but until the app is fully working I can't be sure that this is a valid workaround (I've reported this separate issue on the morphia mailing list: http://groups.google.com/group/morphia/browse_thread/thread/b74cca98a021db1a# )

Thanks.