字段名称“ClassName”被morphia插入mongodb [英] a field name "ClassName" is being inserted into mongodb by morphia

查看:237
本文介绍了字段名称“ClassName”被morphia插入mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mongodb和morphia很新,在开始使用它之后,我意识到mongodb文档中有一个额外的部分,该记录包含一个字段名ClassName,其值为morphia映射的类。

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" with value of the class that morphia mapped.

{ "_id" : ObjectId("51e7a85e300441e5885316c0"), "className" : "models.User", "imgurl" : "", "uname" : "alex"}

这是正常的吗?我觉得它违反了数据库中数据的完整性,无论如何都要绕过它?

is this normal? i feel it is violating the integrity of the data in the db, is there anyway to get around it?

推荐答案

你可以明确地禁用 className 属性: @Entity(noClassnameStored = true)

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).

如果我没有弄错的话,Morphia即使没有 className (基于反射),也足够聪明地根据属性猜测正确的子类。因此,如果 Person 的子类 Developer 中有 mongoKnowledge 属性,并且您的子类 Manager 没有它,Morphia将知道 mongoKnowledge 的文档必须属于 Developer 类。严格来说,只有当您的子类具有相同的属性时才需要 className ;但是,我不会太依赖它。

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.

我没有尝试重命名实体类,但它可能会导致问题。提供 @Polimorphic 注释并将其与之一起修复是一个长期存在的问题。请参见 https://code.google.com/p/morphia/issues/详情?id = 22

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

这篇关于字段名称“ClassName”被morphia插入mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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