参考无法获取(Mongodb/Morphia)。

huangapple 未分类评论45阅读模式
英文:

The reference could not be fetched for (Mongodb/Morphia)

问题

以下是翻译好的内容:

自一个月前起,我在我的Java项目中遇到了一个问题,就是当我启动应用程序时,我会得到这个错误:

启动时出错
    https://pastebin.com/raw/sKszKp4K
还有这个错误:
    https://pastebin.com/raw/4m2KzBHv

这个问题只发生在一些用户身上,不是所有用户都有,而且只发生在一些包含用户的“派系”中。我知道这个错误已经在这里发布过,但我找不到解决方法,我遇到这个错误已经有很长一段时间了,现在不知道该怎么办 参考无法获取(Mongodb/Morphia)。

当我停止应用程序时,会抛出这个错误
    https://pastebin.com/hX1R3nza
我的 FactionUser.java 类:
    https://pastebin.com/6Z68pULe
我的 PlayerFaction.java 类:
    https://pastebin.com/U7Zj0kGC

非常抱歉没有将代码粘贴在这里,因为不允许我超过30000个字符。提前感谢你的帮助!

英文:

Since a month ago i am facing a trouble with my Java project which is that when i start the application i get this:

Error on start
    https://pastebin.com/raw/sKszKp4K
And i also get this:
    https://pastebin.com/raw/4m2KzBHv

This happens for some users, not to all users, and it happens to for some "factions" that have the users inside, i know that this error has been published on here but i cannot find the solution for this, i am having this error since a lot of time ago and i don't know what to do now 参考无法获取(Mongodb/Morphia)。

When i stop the application it throws this error
    https://pastebin.com/hX1R3nza
My FactionUser.java class:
    https://pastebin.com/6Z68pULe
My PlayerFaction.java class:
    https://pastebin.com/U7Zj0kGC

I'm sorry for not pasting the code here but it doesn't allow me to put more than 30000 chars
Thanks in advance!

答案1

得分: 0

很难准确地说出现了什么问题,但我可以提供一些想法,看看是否能帮助你。对于缺少引用错误,可能有以下至少两种情况:

  1. 引用的文档/实体在保存该引用后已被删除。MongoDB不强制执行引用完整性,因此在实践中经常会发生这种情况,我认为。
  2. 引用的ID值实际上在数据库中不存在,因为它实际上从未被保存。Morphia要求在保存引用实体之前,被引用的实体必须具有ID。这是因为Morphia需要该ID值来写入保存引用实体的文档。通常,在引用实体被保存时生成此ID值。但是,在某些情况下(例如,ID类型不是ObjectId),该ID在被引用实体上手动设置。在这种情况下,即使引用实体从未实际保存,也有可能保存引用实体,因为引用对象中的被引用实体具有ID值。在这种情况下,你实际上引用了一个不存在的内容,最终会导致错误。

如果实际上是在引用保存之后被删除了实体,而且这是可以接受的,你实际上可以告诉Morphia在@Reference 注释本身上忽略丢失的引用。

至于堆栈溢出错误,看起来你的嵌入字段中存在循环,因此你可能要确保正确设置了你的模型。Morphia不支持这样的循环,因此开发人员有责任确保它们不会发生。

英文:

It's hard to say exactly what's going wrong here but I can offer some ideas and see if they lead you anywhere. For the missing reference errors one of at least 2 two things are likely:

  1. The referenced document/entity has been deleted since that reference was saved. MongoDB does not enforce referential integrity so this happens a fair bit in practice, I think.
  2. The referenced ID value does not actually exist in the database because it was never actually saved. Morphia requires that referenced entities have an ID before the referencing entity is saved. This is because Morphia needs that ID value to write to the document for the referencing being saved. Typically, this ID value is generated when the referent is saved. However, in some cases (e.g., the ID type is not ObjectId) that ID is manually set on the referent. In this case, it's possible to then save the referencing entity because the referent has an ID value in the object even though that referent was never actually saved. In this case, you'd have a reference to nothing essentially and you'd end up with that error.

If the entity has, in fact, simply been deleted after the reference was saved and that's ok, you can actually tell Morphia to ignore the missing references on the @Reference annotation itself.

As for the stack overflow, it seems like you have a loop in your embedded fields and so you might want to make sure you're properly setting up your model. Morphia does not support cycles like that and so the burden is on the developer to ensure they don't happen.

huangapple
  • 本文由 发表于 2020年4月9日 15:37:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61116149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定