spring-boot - 无法从 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 + "]";
}
解决方案
只要实体定义正确,deleteById 就应该工作。但是尝试使用查询删除
@Query(value = "id:?0" ,delete = true)
void deleteById(String id);
推荐阅读
- java - 即使在应用程序关闭时也显示意图对话框
- django - 芹菜定期任务多次执行
- mpi - 使用 MPI_Bsend 在多个等级之间进行数据交换
- kotlin - 在 init 块中初始化变量并在 kotlin 中为变量定义一个 setter
- python - 代码不断抛出错误```命令帮助已经是现有命令或别名```为什么会这样,我该如何解决?
- flutter - Flutter:将有状态的小部件组合成一个 Flutter-redux 应用架构
- javascript - 如何重写 HTML 页面以获得完全相同的字段?
- flutter - 如何在模型中拥有对象的 ArrayList
- javascript - 如何在firebase中获取新创建用户的用户ID
- system-verilog - 如何用系统verilog中的所有枚举值制作一个数组?