首页 > 解决方案 > 无法从 solr 按 id 删除数据(使用 SolrCrudRepository),其他功能工作正常 - 添加数据、getbyId、getAll

问题描述

这是控制器和实体类。FindById 工作正常,但数据没有被删除。我正在将我的存储库接口扩展到 SolrCrudRepository。由于 findById 工作正常,我认为在获取数据以删除 solr 功能时没有任何问题。- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


 package com.third.solrspring.controller;

    import java.util.ArrayList;
    import java.util.List;

    import javax.annotation.PostConstruct;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;

    import com.third.solrspring.model.Order;
    import com.third.solrspring.repository.Repository;

    @RestController
    public class Controller {
        @Autowired
        private Repository orderRepository;



        @PostMapping("/order")
         public String createOrder(@RequestBody Order order) {
          String description = "Order Created";
          orderRepository.save(order);
          return description;
         }

         @GetMapping("/order/{orderid}")
         public Order readOrder(@PathVariable Long orderid) {
             System.out.println(orderid);
          return orderRepository.findByOrderid(orderid);
         }

         @DeleteMapping("/order/{orderid}")
         public String deleteOrder(@PathVariable Long orderid) {
          String description = "Order Deleted";
          orderRepository.delete(orderRepository.findByOrderid(orderid));
          return description;
         }

         @GetMapping("/order/getAll")
         public Iterable<Order> getOrders()
         {
             return orderRepository.findAll();
         }

    }
    ___________________________________________________________________
    Entity Class

    package com.third.solrspring.model;

    import org.apache.solr.client.solrj.beans.Field;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.solr.core.mapping.SolrDocument;

    @SolrDocument(collection = "Order")
    public class Order {

        @Id
        @Field
        private long orderid;
        @Field
        private String productName;
        @Field
        private String customerName;
        public long getOrderid() {
            return orderid;
        }
        public void setOrderid(long orderid) {
            this.orderid = orderid;
        }
        public String getProductName() {
            return productName;
        }
        public void setProductName(String productName) {
            this.productName = productName;
        }
        public String getCustomerName() {
            return customerName;
        }
        public void setCustomerName(String customerName) {
            this.customerName = customerName;
        }

        @Override
        public String toString() {`enter code here`
            return "Order [orderid=" + orderid + ", productName=" + productName + ", customerName=" + customerName + "]";
        }

标签: spring-bootjpasolr

解决方案


只要实体定义正确,deleteById 就应该工作。但是尝试使用查询删除

  @Query(value = "id:?0" ,delete = true)
   void deleteById(String id);

推荐阅读