如何修复无法找到类型 class java.lang.Long 的 PersistentEntity

How to fix Couldn't find PersistentEntity for type class java.lang.Long

我的界面中有以下扩展 MongoRepository:

@Query(value = "{ '_id': { '$in': ?0} }, { '_id': 1}")
List<Long> getExistingIds(List<String> ids);

当我 运行 这个时,我得到:

org.springframework.data.mapping.MappingException: Couldn't find
PersistentEntity for type class java.lang.Long

我该如何解决这个问题?

使用Json指定投影:

@Query(value = "{ '_id': { '$in': ?0} }", fields = "{ '_id': 1}")
List<YourEntity> getExistingIds(List<Long> ids);
  • YourEntity 将只包含指定的字段。
  • 另请注意,您有 Long id,因此我相应地更改了 ids 参数的类型。

相关文档:MongoDB JSON-based Query Methods and Field Restriction