mysql - 为什么只有部分信息上传到数据库?
问题描述
看到许多由这种类型提出的问题,尽管在我的情况下没有一个真正得到回答。我正在制作一个简单的弹簧启动服务器,它应该像酒的存储一样工作。我的休息电话之一是接受订单,将其写入数据库并更新产品。我设法按顺序正确地编写了部分,尽管即使没有错误迹象,我的产品也没有任何变化(另一个休息调用在与更新产品和工作正常的部分几乎相同的代码上运行) . 我没有想法,但我的假设是查询是错误的,据说我仍然找不到究竟是什么错误。
这是我的一些代码:
控制器:
@RequestMapping(method = RequestMethod.PUT, value = "/new")
public void newOrder(@RequestBody Orders order) {
orderService.newOrder(order);
}
服务实施:
@Override
public void newOrder(Orders order) {
for (Product product : order.getProduct()) {
productRepository.orderProducts(product.getId(), product.getAmount());
}
Orders newOrder = new Orders(
order.getProduct());
ordersRepository.save(newOrder);
}
产品仓库:
@Modifying
@Query("update Product p set p.amount = (p.amount - :amount) where p.id = :id")
void orderProducts(int id, int amount);
订单实体:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@CreationTimestamp
private Timestamp date;
@ManyToMany(mappedBy = "orders")
private List<Product> product;
产品实体:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String brandName;
private int price;
private int mass;
private int alcohol;
@Lob
private byte[] picture;
private int amount;
@ManyToMany
private List<Orders> orders;
我休息一下邮递员:
{
"product": [
{
"id": 1,
"brandName": "pivo",
"price": 23,
"mass": 1,
"alcohol": 47,
"amount": 123,
"orders": []
},
{
"id": 2,
"brandName": "pivo",
"price": 23,
"mass": 1,
"alcohol": 47,
"picture": null,
"amount": 58,
"orders": []
}
]
}
解决方案
我认为您错过了注释中的value
属性。@Query
将您的查询更改为:
@Query( value = "update Product p set p.amount = (p.amount - :amount) where p.id = :id")
推荐阅读
- debugging - Blazor WebAssembly - 无法调试包含某些特定类型代码的程序集
- reactjs - 状态在改变,但颜色没有改变
- android - 在模拟器上运行应用程序不会显示为 android studio 中的 XML 设计页面
- python - 使用 openpyxl 将图表数据标签格式化为 1dp 的百分比
- c - 移位在最高有效数字处给出 1
- typescript - abc是什么
在打字稿中 - javascript - 如何将函数挂钩转换为类状态
- ruby - Ruby - 使用惰性求值找到第一个 N 回文素数
- node.js - Node Express 在 Windows 中使用路径中的反斜杠发送获取静态文件的请求
- kubernetes - 如何为 kubectl 服务创建指定选择器?