MongoDB Morphia仅保存1个用户

时间:2016-09-11 15:11:42

标签: java mongodb morphia

嗨,大家好,我有使用morphia for mongodb的麻烦这就是我正在创造的。 我为我的中心服务器创建了一个插件插件,并使用带有morphia的mongodb将我的用户对象存储到我的集合中,此对象只存储1个用户,而不是将所有用户保存到集合中。

我的用户对象

@Entity(value = "clientdata", noClassnameStored = true)
public class ClientData {

@Id
public int id;

@Indexed(options = @IndexOptions(unique = true))
private String uuid;

@Indexed
private String lastName, username, lastLoginDate, ip;

@Indexed
private int level, exp, joins, coins, pearls;

@Property("hats")
private List<Integer> hatsOwned;

public ClientData(){
    this.hatsOwned = new ArrayList<>();

    if(this.hatsOwned.isEmpty()){
        this.hatsOwned.add(0);
    }
}

public int getId() {
    return id;
}

public String getIp() {
    return ip;
}

public void setIp(String ip) {
    this.ip = ip;
}

public void setId(int id) {
    this.id = id;
}

public String getUuid() {
    return uuid;
}

public void setUuid(String uuid) {
    this.uuid = uuid;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public int getLevel() {
    return level;
}

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

public int getExp() {
    return exp;
}

public void setExp(int exp) {
    this.exp = exp;
}

public int getJoins() {
    return joins;
}

public void setJoins(int joins) {
    this.joins = joins;
}

public void addJoins(int joins) {
    this.joins += joins;
}

public int getCoins() {
    return coins;
}

public void setCoins(int coins) {
    this.coins = coins;
}

public int getPearls() {
    return pearls;
}

public void setPearls(int pearls) {
    this.pearls = pearls;
}

public List<Integer> getHatsOwned() {
    return hatsOwned;
}

public void setHatsOwned(List<Integer> hatsOwned) {
    this.hatsOwned = hatsOwned;
}

public void addNewHatOwned(int hatID){
    this.hatsOwned.add(hatID);
}

public String getLastLoginDate() {
    return lastLoginDate;
}

public void setLastLoginDate(String lastLoginDate) {
    this.lastLoginDate = lastLoginDate;
}
}

我的mongo经理班

public class MongoManager {
private static MongoManager ins = new MongoManager();

private MongoClient mc;
private Morphia morphia;
private Datastore datastore;
private ClientDAO userDAO;

public static MongoManager get() {
    return ins;
}

public void init() {
    ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
    List<MongoCredential> credentials = new ArrayList<>();
    credentials.add(MongoCredential.createCredential("union", "admin", "union16".toCharArray()));
    mc = new MongoClient(addr, credentials);

    morphia = new Morphia();

    morphia.map(ClientData.class);

    datastore = morphia.createDatastore(mc, "admin");
    datastore.ensureIndexes();

    userDAO = new ClientDAO(ClientData.class, datastore);
}

public void disconnect(){
    this.mc.close();
}

public ClientData getUserByPlayer(Player player) {
    ClientData du = userDAO.findOne("uuid", player.getUniqueId().toString());
    long time = System.currentTimeMillis();

    if (du == null) {
        du = new ClientData();
        du.setUuid(player.getUniqueId().toString());
        du.setCoins(0);
        du.setExp(0);
        du.setJoins(0);
        du.setLastLoginDate(DateFormat.getTimeInstance().format(time));
        du.setLevel(0);
        du.setPearls(0);
        du.setIp(player.getAddress().getAddress().toString());
        du.setUsername(player.getDisplayName());
        du.setLastName(player.getName());
        du.setHatsOwned(null);
        userDAO.save(du);
    }
    return du;
}


public void saveUser(ClientData user) {
    userDAO.save(user);
}

public List<ClientData> getAllUsers() {
    return userDAO.find().asList();
}
}

1 个答案:

答案 0 :(得分:0)

您不能插入多个,因为您有duplicateKey:您不能在不设置唯一值的情况下使用基本类型的@id

您定义了这样的ID:

@Id
public int id;

即使您从未为id设置任何值,基本类型也会初始化为0,因此您最终会尝试使用相同的键插入多个文档。

解决方案:

你可以:

  • 将您的id更改为字符串@Id String id;
  • 将您的id更改为 ObjectId @Id ObjectId id;
  • 保留@Id int id并手动为其设置唯一值(例如player.getUniqueId())。

前两个选项将起作用,因为它们不会被初始化并且将为null。然后Mongo将为您生成一个唯一的ID。