【问题标题】:How to build a search endpoint in spring data MongoRepository如何在 Spring Data MongoRepository 中构建搜索端点
【发布时间】:2019-01-24 08:03:53
【问题描述】:

我正在尝试使用 Spring Data MongoRepository 访问 mongodb 数据层。所以在这里我可以使用存储库端点进行基本的 CRUD 操作,但无法进行自定义搜索。

模型类:

@Document(collection = "merchant")
public class Merchant {

 @Id
 private String id;
 private Long zohoAccountRefId;
 private String businessId;
 private String businessName;
 private String businessAddress;
 private String businessPhone;
 private String description;
 private String businessEmail;
 private String accountType;
 private BusinessOwner businessOwner;
 private List<Product> products;
 private List<Plugin> plugins;
 private List<Service> services;
 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
 private Date dateCreated;

 //getters and setters
}

存储库:

@RepositoryRestResource(collectionResourceRel = "account",path = "account")
public interface MerchantRepository extends MongoRepository<Merchant,String> {

  @RestResource(path = "businessName",rel = "businessName")
  List<Merchant> findByName(@Param("businessName") String businessName);
 }

当我尝试使用此代码时,我收到以下错误:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property name found for type Merchant!

这是实现此功能的正确方法吗?或者我得到的这个问题的解决方案是什么?

【问题讨论】:

    标签: spring-data spring-data-mongodb spring-data-rest spring-repositories


    【解决方案1】:

    方法的名称很重要,它告诉 Spring Data MongoDB 如何构建查询。您已将方法命名为 findByName,因此 Spring Data MongoDB 正在尝试针对名为 name 的属性创建查询,但您的 Merchant 集合中没有任何简单命名为 name 的属性。

    要查询Merchant.businessName,您的方法应该是:

    List<Merchant> findByBusinessName(@Param("businessName") String businessName);
    

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 2021-08-27
      • 1970-01-01
      • 2015-12-23
      • 2019-04-19
      • 2020-11-03
      • 1970-01-01
      • 2017-10-24
      • 2014-10-30
      相关资源
      最近更新 更多