当dao将DBRef与Map一起使用时,使用MongoRepository构建查找和计数查询

时间:2016-07-12 08:01:59

标签: mongodb dbref mongorepository

是什么名字" count"并且"找到"当我将DBRef与java.util.map一起使用时查询?

在我的例子中,countByFoos-Method总是返回0而不是1。

如何通过给定的FooDao找到并统计BarDaos?

public class FooDao {

   @Id
   private String id;

   private String key; 

   ...
}

public class BarDao {

   @Id
   private String id;

   @DBRef
   private Map<String, FooDao> foos;

   ...
}

public interface FooRepository extends MongoRepository<FooDao, String> {
   ...
}

public interface BarRepository extends MongoRepository<BarDao, String> {

   long countByFoos(FooDao fooDao);

   ...
}

public void doSomthing() {
   FooDao foo = new FooDao("fooId1", "key1");
   fooRepository.save(foo);

   BarDao bar = new BarDao("barId1");
   bar.addFoo(foo.getKey(), foo);
   barRepository.save(bar);

   int count = barRepository.countByFoos(foo);
}

0 个答案:

没有答案