可以使用MongoRepository从另一个集合检索数据吗?

huangapple go评论66阅读模式
英文:

can i retrieve data from another collection using MongoRepository?

问题

我有一个名为"Invoice"的类和一个MongoRepository,我想从我的Mongo数据库中提取所有已验证的发票(在给定时间范围内创建的发票)。以下是我的Mongo存储库代码:

import java.util.Date;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import Invoices.Invoice;

@Repository
public interface InvoiceRepositoryMongo extends MongoRepository<Invoice, Integer> {

    @Query("db.invoices_bis.find({createdAt : {$gte : new ISODate('2013-04-30T17:24:16.000+00:00') , $lte : new ISODate('2013-05-30T17:24:16.000+00:00')}})")
    List<Document> testrequete(Date start, Date ed);
}

不要太关注查询,它只是用于测试的。但问题是,当我运行这个代码时,我遇到了这个错误:
"nested exception is org.springframework.data.mapping.PropertyReferenceException: No property testrequete found for type Invoice!"

我认为问题在于该方法返回了一个<Document>列表,但我不确定。谢谢!

英文:

i have a class called "Invoice" and a MongoRepository
and what i want is to extract from my mongo database all validated invoices (those created in a given time range)
so here is my mongo repository :


import java.util.Date;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import Invoices.Invoice;


@Repository
public interface InvoiceRepositoryMongo extends MongoRepository&lt;Invoice,Integer&gt;{

		@Query(&quot;db.invoices_bis.find({createdAt : {$gte : new ISODate(&#39;2013-04-30T17:24:16.000+00:00&#39;) , $lte : new ISODate(&#39;2013-05-30T17:24:16.000+00:00&#39;)}})&quot;) 
		List&lt;Document&gt; testrequete(Date start, Date ed);
}

dont pay too much attention to the query it is just for testing , but the problem is when i run this , i have this error :
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property testrequete found for type Invoice!

i think the problem is that the method return a list of <Document> but i'm not sure

thanks !

答案1

得分: 1

我认为问题出在你的实体称为 Invoice 的部分,

MongoRepository<Invoice, Integer>

所以结果应该类似于:

List<Invoice> testrequete(Date start, Date ed);

英文:

i think te problem is that your Entity calls Invoice,

MongoRepository&lt;Invoice,Integer&gt;

so the result should be something like :

List&lt;Invoice&gt; testrequete(Date start, Date ed);

huangapple
  • 本文由 发表于 2020年8月8日 23:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63317387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定