[英]spring data and mongodb with @Query annotation for an array
I have the class: 我上课:
public class Game {
@Id
private Integer gameId;
private int prize;
private int[] numbers;
//gets and sets
}
then I have the mongorepository class: 然后我有mongorepository类:
public interface GameRepository extends MongoRepository<Game, Integer> {
@Query(value = "{ 'numbers' : {$all : [?0] }}")
public List<JogoLotoFacil> myFind(int[] numbers);
}
When I execute the query straight onto mongodb I get the results that I want, but running on java with spring data I always get an empty list. 当我直接在mongodb上执行查询时,我得到了想要的结果,但是在带有spring数据的java上运行,我总是得到一个空列表。
If I run findAll() I get the correst list. 如果我运行findAll(),我会得到相应的清单。
Question is: is it possible to use @Query for arrays? 问题是:可以对数组使用@Query吗?
The custome @Query
annotation is buggy for some operators. @Query
注释对于某些操作员而言是错误的。 Would suggest to create your own custom interface and your implementation class to execute the custom query using the MongoTemplate. 建议您创建自己的自定义接口和实现类,以使用MongoTemplate执行自定义查询。 For example, create an interface with a name that appends Custom: 例如,创建一个名称后附加“自定义”的接口:
public interface GameRepositoryCustom {
public List<JogoLotoFacil> myFind(int[] numbers);
}
Modify the GameRepository
and add the GameRepositoryCustom
interface to be extended: 修改GameRepository
并添加要扩展的GameRepositoryCustom
接口:
@Repository
public interface GameRepository extends GameRepositoryCustom, MongoRepository {
}
Create your implementation class to implement the methods defined in GameRepositoryCustom
interface. 创建实现类以实现GameRepositoryCustom
接口中定义的方法。
public class GameRepositoryImpl implements GameRepositoryCustom {
@Autowired
MongoTemplate mongoTemplate;
@Override
public List<JogoLotoFacil> myFind(int[] numbers) {
return mongoTemplate.find(
Query.query(Criteria.where("numbers").all(numbers)), Game.class);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.