问题描述
我遇到了一个很有趣的问题.尝试从Mongo实例加载模型时,Morphia会引发以下错误:
I'm having a rather interesting issue. When attempting to load a model from a Mongo instance, Morphia throws the following error:
[22:17:13 WARN]: Class not found defined in dbObj:
java.lang.ClassNotFoundException: me.mattrick.test.storage.TestStat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_101]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_101]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_101]
at java.lang.Class.forName(Class.java:348) ~[?:1.8.0_101]
at org.mongodb.morphia.mapping.DefaultCreator.getClass(DefaultCreator.java:175) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:97) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:205) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:142) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:45) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:772) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:191) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:220) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.dao.BasicDAO.findOne(BasicDAO.java:180) [Test-1.5-SNAPSHOT-shaded.jar:?]
at me.mattrick.test.storage.UserRepository.getUserFromPlayer(UserRepository.java:22) [Test-1.5-SNAPSHOT-shaded.jar:?]
at me.mattrick.test.event.JoinEvent.onJoin(JoinEvent.java:37) [Test-1.5-SNAPSHOT-shaded.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
[22:17:13 WARN]: Class not found defined in dbObj:
java.lang.ClassNotFoundException: me.mattrick.test.storage.TestStat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_101]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_101]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_101]
at java.lang.Class.forName(Class.java:348) ~[?:1.8.0_101]
at org.mongodb.morphia.mapping.DefaultCreator.getClass(DefaultCreator.java:175) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:87) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:102) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:205) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:142) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:45) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:772) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:191) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:220) [Test-1.5-SNAPSHOT-shaded.jar:?]
at org.mongodb.morphia.dao.BasicDAO.findOne(BasicDAO.java:180) [Test-1.5-SNAPSHOT-shaded.jar:?]
at me.mattrick.test.storage.UserRepository.getUserFromPlayer(UserRepository.java:22) [Test-1.5-SNAPSHOT-shaded.jar:?]
at me.mattrick.test.event.JoinEvent.onJoin(JoinEvent.java:37) [Test-1.5-SNAPSHOT-shaded.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
如您所见,我正在Spigot服务器(实际上是具有适当API实现的Minecraft服务器)中运行它.我不确定这是否非常相关,但是Spigot服务器以类似于Morphia的方式加载插件,所以我认为这可能是导致此问题的原因,因为以前有记录的Morphia实例与其他实例冲突使用类加载器的库.
As you can see, I am running it inside a Spigot server (which is essentially a Minecraft server with a proper API implementation). I'm not sure if this is terribly relevant, but the Spigot server loads plugins in a similar way that Morphia would, so I think it may be possible that this is causing this issue, since there's been previous documented instances of Morphia conflicting with other libraries that use class loaders.
在@Entity批注中添加"noClassnameStored = true"似乎可以防止此错误,但是我在数据库内部存储了一系列自定义对象,这也会导致此错误.
Adding "noClassnameStored = true" to the @Entity annotation seems to prevent this error, however I am storing a list of custom objects inside the database, which also causes this error.
我正在使用的模型如下所示:
The models I am using can be seen below:
Stat.java:
Stat.java:
package me.mattrick.test.storage;
public class Stat {
}
TestStat.java:
TestStat.java:
package me.mattrick.test.storage;
import lombok.Getter;
import lombok.Setter;
public class TestStat extends Stat {
@Getter @Setter
private int test = 100;
}
User.java:
User.java:
package me.mattrick.test.storage;
import lombok.Getter;
import lombok.Setter;
import me.mattrick.test.Test;
import me.mattrick.test.api.storage.mongo.MongoEntity;
import me.mattrick.test.cosmetics.particles.EnumParticleEffect;
import me.mattrick.test.player.PlayerRank;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.mongodb.morphia.annotations.Entity;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Entity(value = "users", noClassnameStored = true)
public class User extends MongoEntity {
@Getter @Setter
private UUID uuid;
@Getter @Setter
private PlayerRank rank = PlayerRank.DEFAULT;
@Getter @Setter
private EnumParticleEffect effect = EnumParticleEffect.NONE;
@Getter @Setter
private int money = 0;
@Getter @Setter
private int level = 0;
@Getter @Setter
private long experience = 0;
@Getter @Setter
private LocalDateTime firstJoined;
@Getter @Setter
private LocalDateTime lastPlayed;
@Getter @Setter
private List<Stat> stats = new ArrayList<>();
public Player getPlayer() {
return Bukkit.getPlayer(uuid);
}
public void save() {
Test.getInstance().getMongoBridge().repositoryBy(User.class).save(this);
}
public <T extends Stat> T getStat(Class<T> statClass) {
for (Stat stat : stats) {
if(stat.getClass() == statClass) {
return statClass.cast(stat);
}
}
return null;
}
public void addStat(Stat stat) {
stats.add(stat);
}
}
MongoEntity.java:
MongoEntity.java:
package me.mattrick.test.api.storage.mongo;
import lombok.Getter;
import lombok.Setter;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Id;
public class MongoEntity {
@Id
@Getter @Setter
private ObjectId id;
}
应该注意,由于在@Entity批注上设置了"noClassnameStored = true"参数,因此User类可以保存和加载得很好,但是Stat类却不能,因为这样做不可行.
It should be noted that the User class saves and loads fine as a result of the "noClassnameStored = true" parameter set on the @Entity annotation, but the Stat class does not, since this is not feasible for this.
有人会碰巧知道解决方案吗?这是一个相当奇怪的错误,我对Morphia不太了解.感谢您为如此长的帖子所提供的帮助和歉意.
Would anyone happen to know the solution to this? Its a rather odd error, and I'm not too experienced with Morphia. Thank you for you assistance and apologies for such a long post.
推荐答案
我设法弄清楚了.您需要使用您自己的插件覆盖Morphia的ClassLoader.如果有人使用Spigot或Bukkit寻找答案:
I've managed to figure it out. You need to override Morphia's ClassLoader with your plugin's own. In case anyone using Spigot or Bukkit are looking for the answer:
morphia.getMapper().getOptions().setObjectFactory(new DefaultCreator() {
@Override
protected ClassLoader getClassLoaderForClass() {
return JavaPlugin.getClassLoader();
}
});
这篇关于Morphia-找不到在dbObj中定义的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!