gpt4 book ai didi

java - Morphia - BasicDBObject 无法转换为 MyClass

转载 作者:行者123 更新时间:2023-12-02 00:53:28 24 4
gpt4 key购买 nike

我正在尝试使用 Morphia 将 HashMap 嵌入到我的 MongoDB 中。在与此处的帖子相同的警告之后:

Morphia - Class not found defined in dbObj注意:为了彻底起见,这篇文章中的建议没有帮助。

我终于弄清楚,如果我向嵌入字段添加 @Property 属性,它就会抑制警告。

不幸的是,这现在导致了一个问题,我似乎没有一个 HashMap 对象的数组,而是一个无法转换回我的 HashMap 类型的 BasicDBObject 数组

上述帖子的答案还建议重写 morphia 的类加载器。不幸的是,他们在示例中使用的 getter 现在受到保护,因此我现在无法返回 JavaPlugin.getClassLoader()

完整错误:

[23:29:01 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'quest' in plugin ExploreRPG v1.0-SNAPSHOT
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.14.4.jar:git-Paper-176]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.14.4.jar:git-Paper-176]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:739) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1800) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1608) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990) ~[patched_1.14.4.jar:git-Paper-176]
at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923) ~[patched_1.14.4.jar:git-Paper-176]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
Caused by: java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to net.exploremc.explorerpg.controllers.user.QuestProgress
at net.exploremc.explorerpg.controllers.User.getActiveQuests(User.java:148) ~[?:?]
at net.exploremc.explorerpg.commands.QuestCMD.onCommand(QuestCMD.java:23) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.14.4.jar:git-Paper-176]
... 17 more

用户类别:

@Entity(value = "users", noClassnameStored = true)
public class User {
@Transient public static HashMap<UUID, User> usersList = new HashMap<>();
@Transient private static Datastore datastore = ExploreRPG.getDatabase().getDatastore();

@Id private String id;
@Transient private Player player;
@Indexed private String uuid;

private List<String> ipHistory = new ArrayList<>();
private List<String> nameHistory = new ArrayList<>();
@Embedded @Property("quests") private HashMap<String, QuestProgress> quests = new HashMap<>(); // This is the problem class

public User(){ }

public User(Player player) {
this.player = player;
this.ipHistory.add(player.getAddress().getHostString());
this.uuid = player.getUniqueId().toString();
this.nameHistory.add(player.getName());
}

public void storeUser() {
datastore.save(this);
}

public void updateUser(){
Query<User> updateQuery = datastore.createQuery(User.class).field("uuid").equal(uuid);
UpdateOperations<User> ops = datastore.createUpdateOperations(User.class)
.set("ipHistory", ipHistory)
.set("nameHistory", nameHistory)
.set("quests", quests);
datastore.update(updateQuery, ops);
}

public static boolean doesUserExist(Player player){

Query<User> query = datastore.find(User.class).field("uuid").equal(player.getUniqueId().toString());
User user = query.first();
return user != null;
}

public static User getUserFromDB(Player player){
Query<User> query = datastore.find(User.class).field("uuid").equal(player.getUniqueId().toString());
User user = query.first();
user.quests = query.first().quests;
user.player = player;
return user;
}
}

导致此错误的实现:

public List<QuestProgress> getActiveQuests(){
List<QuestProgress> activeQuests = new ArrayList<>();
for (QuestProgress quest : quests.values()) {
if(!quest.isCompleted())
activeQuests.add(quest);
}
return activeQuests;
}

我希望能够循环遍历 quest HashMap ,但是程序在进入 foreach 循环时失败了。检查数据类,显示 map 数据不符合预期。相反,该值是 BasicDBObject 类型,我相信这就是导致我的错误的原因 /image/3herm.png

我尝试将这个新结构重新类型转换到我的原始 map 中,但没有成功。

我使用的是 Morphia core 1.5.3mongo-java-驱动程序版本 3.11.0使用 Spigot 1.14.4 API

感谢任何帮助

最佳答案

您是否尝试过在该类型上显式调用 map() ? Morphia 可以执行一些延迟映射,但这可能是一个被破坏的代码路径。

关于java - Morphia - BasicDBObject 无法转换为 MyClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com