Morphia/Mongo 无法链接 @Reference,给出:无法获取参考

问题描述

我已经建立了一个简单的数据存储来存储版本和构建。这是他们的课程-

@Entity("Version")
public class Version {
    @Id
    private ObjectId id = new ObjectId();
    public Version() {}
    private String name;
}
@Entity("Build")
public class Build {
    public Build() {}
    @Id
    ObjectId id = new ObjectId();
    @Reference
    Version version;
    String name;    
}

(类中的字段实现了它们的 getter、setter、构造函数和 toString)。

现在我尝试先简单地存储版本,然后尝试存储构建,然后读取存在的所有构建。

Version version = new Version("first");
Version version2 = new Version("second");

VersionDAO.saveVersion(version);
VersionDAO.saveVersion(version2);
VersionDAO.printAllVersions();

Build build = new Build(version,"Hello");
BuildDAO.saveBuild(build);
BuildDAO.getAllBuilds();

VersionDAO.getAllVersions() 工作正常,并为我获取存储的版本列表,但 BuildDAO.getAllBuilds() 抛出错误说-

线程“main”dev.morphia.mapping.MappingException 中的异常:无法在数据库“Builds”中映射带有 ID:602f4e9ff760cd5638698273 的itting_morphia.Build

Caused by: dev.morphia.mapping.MappingException: The reference ({ "$ref" : "Version","$id" : "602f4e7b96f28d54bd1cc883" }) 无法为 Try_morphia.Build.version 获取

似乎有一些我似乎无法弄清楚的参考链接问题。我必须使用@Reference,因为这段代码是更大片段的一部分,我无法更改它的全部内容

MongoDB Compass 将 Build 条目的值显示为-

_id: ObjectId("602f4e9ff760cd5638698273")  
className: "trying_morphia.Build"  
version: DBRef(Version,602f4e7b96f28d54bd1cc883,undefined)  
name:"Hello"  

参考文献中我遗漏了什么?

我尝试过的:将 Version 和 Build 的 id 更改为字符串。

解决方法

Morphia 不支持跨数据库引用。您需要手动管理这些引用。