morphia mongodb的InstantiationException

时间:2017-08-17 23:31:23

标签: java mongodb abstract morphia

我试图将一个类嵌入到一个Set中并在morphia实体上插入该组,但它返回了这个堆栈跟踪。

我第一次在morphia实体中使用抽象类时也是第一次看到这个错误。

我使用了一个抽象类,因为我需要在玩家通过执行/套件选择他的套件时触发执行方法,玩家拥有的所有套件都存储在一组中并插入到我的mongodb播放器文档中并保存但是当试图触发执行方法,它返回该错误。

Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[?:1.8.0_131]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_131]
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:72) ~[?:?]
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:91) ~[?:?]
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:105) ~[?:?]
    at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:209) ~[?:?]
    at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:144) ~[?:?]
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:47) ~[?:?]
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850) ~[?:?]
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:282) ~[?:?]
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:193) ~[?:?]
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134) ~[?:?]
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146) ~[?:?]
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117) ~[?:?]
    at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:236) ~[?:?]
    at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:227) ~[?:?]
    at org.mongodb.morphia.dao.BasicDAO.findOne(BasicDAO.java:178) ~[?:?]
    at eu.unionmc.kitpvp.player.PlayerManager.get(PlayerManager.java:49) ~[?:?]
    at eu.unionmc.kitpvp.commands.kitCommand.onCommand(kitCommand.java:31) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-550ebac-7019900]

玩家 - Morphia实体

@Entity(value = "users", noClassnameStored = true)
public class KPlayer extends User{

private int kills, deaths, level;
private long gold;

private PlayerRank currentRank;

private Set<Kit> ownedKits = new HashSet<>();

@Transient
private float kdr;

public int getKills() {
    return kills;
}

public void setKills(int kills) {
    this.kills = kills;
}

public int getDeaths() {
    return deaths;
}

public void setDeaths(int deaths) {
    this.deaths = deaths;
}

public float getKdr() {
    return kdr;
}

public void setKdr(float kdr) {
    this.kdr = kdr;
}

public PlayerRank getCurrentRank() {
    return currentRank;
}

public void setCurrentRank(PlayerRank currentRank) {
    this.currentRank = currentRank;
}

public int getLevel() {
    return level;
}

public void setLevel(int level) {
    this.level = level;
}

public long getGold() {
    return gold;
}

public void setGold(long gold) {
    this.gold = gold;
}

public Set<Kit> getOwnedKits() {
    return ownedKits;
}

public void setOwnedKits(Set<Kit> ownedKits) {
    this.ownedKits = ownedKits;
}

public void addKit(Kit kit){
    this.ownedKits.add(kit);
}

public boolean ownKit(Kit kit){
    return this.ownedKits.contains(kit);
}
}

套件类

package eu.unionmc.kitpvp.kit;

import org.bukkit.Material;

public abstract class Kit{

private String name;
private String[] description;

private boolean locked;
private int levelToApply;

private long cost;

private Material icon;

public Kit() {
}

public Kit(String name, boolean locked, int levelToApply, long cost, Material icon, String... description) {
    this.name = name;
    this.description = description;
    this.locked = locked;
    this.levelToApply = levelToApply;
    this.cost = cost;
    this.icon = icon;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String[] getDescription() {
    return description;
}

public void setDescription(String[] description) {
    this.description = description;
}

public boolean isLocked() {
    return locked;
}

public void setLocked(boolean locked) {
    this.locked = locked;
}

public int getLevelToApply() {
    return levelToApply;
}

public void setLevelToApply(int levelToApply) {
    this.levelToApply = levelToApply;
}

public Material getIcon() {
    return icon;
}

public void setIcon(Material icon) {
    this.icon = icon;
}

public long getCost() {
    return cost;
}

public void setCost(long cost) {
    this.cost = cost;
}

public abstract void apply();

}

PlayerManager类

public class PlayerManager implements CRUD<KPlayer,Player> {

private KitPool pool;

public PlayerManager(KitPool pool) {
    this.pool = pool;
}

public KPlayer create(Player p){
    KPlayer kp = alreadyExists(p.getUniqueId().toString());

    if(kp == null){
        kp = new KPlayer();

        kp.setUUID(p.getUniqueId().toString());
        kp.setUsername(p.getName());
        kp.setEntity(p);

        kp.setLevel(0);
        kp.setCurrentRank(PlayerRank.NONE);
        kp.setKdr(0);
        kp.setKills(0);
        kp.setDeaths(0);
        kp.setGold(0);
        kp.addKit(new PVP());
        update(kp);
    }

    return kp;
}

@Override
public KPlayer get(Player entry) {
    return pool.getPlayerRef().findOne("uuid", entry.getUniqueId().toString());
}

@Override
public void update(KPlayer entry) {
    pool.getPlayerRef().save(entry);
}

@Override
public void delete(KPlayer entry) {
    pool.getPlayerRef().delete(entry);
}

@Override
public KPlayer alreadyExists(String entry) {
    return pool.getPlayerRef().findOne("uuid", entry);
}

}

套件命令类

public class kitCommand implements CommandExecutor {

KitPvP plugin;

public kitCommand(KitPvP plugin) {
    this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
    if(!(sender instanceof Player)) return true;

    Player p = (Player) sender;
    KPlayer kp = plugin.getPlayerManager().get(p);

    List<String> kitNames = plugin.getKitManager().getKits().stream().map(Kit::getName).collect(Collectors.toList());
    String names = StringUtils.join(kitNames, ", ");

    if(args.length == 0){
        p.sendMessage("§cUsage: /kit <kitName>");
        p.sendMessage("§eKits: §6"+names);
        return  true;
    }

    if(kp == null) return true;

    if(plugin.getKitManager().getKitByName(args[0]) == null){
        p.sendMessage("§cThis kit doens't exists.");
        return true;
    }

    plugin.getKitManager().perform(p, kp, plugin.getKitManager().getKitByName(args[0]));

    return false;
}
}

1 个答案:

答案 0 :(得分:0)

为KPlayer提供默认的无args构造函数auth公共访问权限,并且应修复此异常