a field name “ClassName” is being inserted into mongodb by morphia

前端 未结 2 1803
夕颜
夕颜 2020-12-17 18:15

I\'m pretty new to mongodb and morphia, after starting using it, I realize that there is a extra part in mongodb document, the record contains a filed name \"ClassName\" wit

2条回答
  •  佛祖请我去吃肉
    2020-12-17 18:43

    You can explicitly disable the className attribute: @Entity(noClassnameStored = true)

    I'm generally suppressing the attribute, if there is a single entity class. If I'm subclassing my entity, I'm explicitly enabling it (which is the default, but just to be sure).

    If I'm not mistaken, Morphia is clever enough to guess the correct subclass based on the properties even if there is no className (based on reflection). So if you have an attribute mongoKnowledge in your subclass Developer of Person and your subclass Manager doesn't have it, Morphia will know that documents with mongoKnowledge must be of the class Developer. So strictly speaking className is only required if your subclasses have the same attributes; however, I wouldn't rely on it too much.

    I haven't tried to rename the entity classes, but it will probably cause issues. There's a long standing issue to provide a @Polimorphic annotation and to fix it together with that. See https://code.google.com/p/morphia/issues/detail?id=22

提交回复
热议问题