保存对象后Morphia 2.0.0-RC1查询returns空列表

Morphia 2.0.0-RC1 query returns empty list just after I have saved an object

我正在尝试让 Morphia 与 mongoDB 一起工作,并且能够将我的对象保存到数据库中。但是当我尝试检索所有对象时,我得到一个空列表?

我正在使用 mongo db java 驱动程序 3.12.0 和吗啡 2.0.0-rc1。旧的 execute 方法自 2.0 以来已被弃用,但参考指南尚未更新。我查看了 github 上的代码,现在看来直接调用迭代器而不是使用 execute before 是正确的方法。

更新:只要添加任何过滤器我就能得到结果?

public class MongoDBConnectorTest {
@Test
public void test() {
    //MongoDatabase db = MongoDBConnector.getClient().getDatabase("lottoDB");
    //MongoCollection<Lotto> lottoCollection = db.getCollection("lotto", Lotto.class);

    Datastore lottoDatastore = LottoDataStore.getLottoDataStore().getDataStore();

    //System.out.println(db.getName());

    final Lotto lotto = new Lotto();
    lotto.setHighestPriceMoney(100000);

    // Insert lotto object
    //lottoCollection.insertOne(lotto);
    lottoDatastore.save(lotto);

    // Find it again
    //final Query<Lotto> query = lottoDatastore.find(Lotto.class);
    //List<Lotto> lottos = (List<Lotto>) query.iterator().toList();
    final Query<Lotto> query = lottoDatastore.find(Lotto.class);
    final List<Lotto> lottos = query.iterator().toList();
    final Lotto lotto2 = query.first();
    //Lotto lotto2 = query.first();

    System.out.println("Lotto found:");
    System.out.println(lottos.size());
    System.out.println(lotto2);

}
}

LottoDataStore.java

public class LottoDataStore {
private static LottoDataStore lottoDataStore = null;
protected final Datastore datastore;

public static LottoDataStore getLottoDataStore() {
    if(lottoDataStore == null) lottoDataStore = new LottoDataStore();

    return lottoDataStore;
}

private LottoDataStore() {
    this.datastore = Morphia.createDatastore(MongoDBConnector.getClient().getMongoClient(), "lottoDB");
    // tell Morphia where to find your classes
    // can be called multiple times with different packages or classes
    datastore.getMapper().mapPackage("com.j.productdata.model.entity.lotto");
    datastore.ensureIndexes();
}

public Datastore getDataStore() {
    return datastore;
}

}

Lotto.java

@Entity("lotto")
public class Lotto {
@Id
protected String alias = "lotto";

@BsonProperty(value = "highest_price_money")
protected int highestPriceMoney;

@BsonProperty(value = "average_price_money")
protected int averagePriceMoney;

@BsonProperty(value = "min_cost")
protected int minCost;

@BsonProperty(value = "min_cost_highest_price_money")
protected int minCostHighestPriceMoney;

List<LottoResult> results;

public Lotto() {

}

public String getAlias() {
    return alias;
}



public void setAlias(String alias) {
    this.alias = alias;
}



public int getHighestPriceMoney() {
    return highestPriceMoney;
}



public void setHighestPriceMoney(int highestPriceMoney) {
    this.highestPriceMoney = highestPriceMoney;
}



public int getAveragePriceMoney() {
    return averagePriceMoney;
}



public void setAveragePriceMoney(int averagePriceMoney) {
    this.averagePriceMoney = averagePriceMoney;
}



public int getMinCost() {
    return minCost;
}



public void setMinCost(int minCost) {
    this.minCost = minCost;
}



public int getMinCostHighestPriceMoney() {
    return minCostHighestPriceMoney;
}



public void setMinCostHighestPriceMoney(int minCostHighestPriceMoney) {
    this.minCostHighestPriceMoney = minCostHighestPriceMoney;
}



public List<LottoResult> getResults() {
    return results;
}



public void setResults(List<LottoResult> results) {
    this.results = results;
}



@Override
public String toString() {
    return "Lotto [id=" + alias + ", highestPriceMoney=" + highestPriceMoney + ", minCost=" + minCost
            + ", minCostHighestPriceMoney=" + minCostHighestPriceMoney + ", results=" + results + "]";
}


}

这适用于我使用 reproducer 和 2.0.0-RC1。我不完全确定您没有看到结果的本地情况。但是,代码中有一些奇怪之处。您的代码中有许多驱动程序注释。就目前而言这很好,但 Morphia 不使用这些注释。

可能是您正在映射的包与 Lotto 所在的位置不匹配,但我预计在这种情况下会出现有关丢失 Codec 的错误。