0

我正在使用 MongoDB 和 java-lib Morphia 来处理它。

我有两个课程:

class User {
   ...

   #Reference
   private List<Invoice> invoiceTransactions = new ArrayList<Invoice>();

   ...
}

class Invoice {
   ...

   User user;

   ...
}

当我创建发票时,我将其放入invoiceTransactions列表

现在我有一个Invoice对象,如果它在invoiceTransactions列表中不包含此发票,我的目标是找到相应的用户。

这就是我写的:

Invoice invoice = ...;

Query<User> query = (Query) dao.createQuery();
query.disableValidation().filter(Mapper.ID_KEY, u.getId());
List<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice);
query.criteria(User.INVOICE_TRANSACTIONS).hasNoneOf(inv);

那是 MongoDB 日志:

Wed Jul 18 19:03:09 [conn48] update dwh.UserImpl query: { _id: ObjectId('4ffbc8d
943b693ac766255f3'), InvoiceTransactions: { $nin: [ { className: "abc.InvoiceImpl", sum: 70.0, invoiceStatus: "NEW", creationDate:
new Date(1342627389037), _id: ObjectId('5006de3d022993ac397609c4'), guid: "4DEAB
CCE-63F9-1B0A-59F2-1135A83054B9", day: "2012.07.18", hour: "2012.07.18 19", minu
te: "2012.07.18 19:03" } ] } } update: { $set: { authId: "bla-bla" } } 0ms

您可以看到Invoice对象处理的不是引用而是嵌入对象。结果,即使实际上invoiceTransactions包含 Invoice,我也会触发该标准。

所以,我的问题是如何强制 morphia 将inv对象视为参考而不将其放入所有字段的结果查询中?

4

1 回答 1

0

好的,这是答案:

Key<Invoice> invoiceKey = dao.getDatastore().getKey(invoice);
List<Key<Invoice>> invoiceKeyList = new ArrayList<Key<Invoice>>();
invoiceKeyList.add(invoiceKey);
query.criteria(User.INVOICE_TRANSACTIONS).hasNoneOf(invoiceKeyList);
于 2012-07-19T08:35:23.990 回答