【问题标题】:Morphia 2.0.0-RC1 query returns empty list just after I have saved an objectMorphia 2.0.0-RC1 查询在我保存对象后返回空列表
【发布时间】:2020-05-31 16:31:24
【问题描述】:

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

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

更新:只需添加任何过滤器即可获得结果?

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;
}

}

乐透.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 + "]";
}


}

【问题讨论】:

    标签: java mongodb morphia


    【解决方案1】:

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

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

    【讨论】:

    • 首先,感谢您与 Morphia 的合作,非常感谢!您对编解码器有什么建议吗?如果我向查询添加过滤器,即 final Query query = lottoDatastore.find(Lotto.class); query.filter(Filters.exists("别名")); final List lottos = query.iterator().toList();它有效。
    • 我发现了问题 - 当我注册 MongoClient 时,我添加了 PojoCoded。没有这个编解码器它可以工作。太棒了,谢谢!
    • Morphia 将为 mongo 客户端配置正确的编解码器提供程序,以找到它为您的实体构建的编解码器。如果您手动添加了PojoCodec,那么注册表可能会发现它,而您正在将您的实体保存在另一个集合中。我猜如果您查看 Lotto 集合而不是 morphia 正在查找的 lotto 集合,您会找到丢失的文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多