首页 > 解决方案 > 为什么只有部分信息上传到数据库?

问题描述

看到许多由这种类型提出的问题,尽管在我的情况下没有一个真正得到回答。我正在制作一个简单的弹簧启动服务器,它应该像酒的存储一样工作。我的休息电话之一是接受订单,将其写入数据库并更新产品。我设法按顺序正确地编写了部分,尽管即使没有错误迹象,我的产品也没有任何变化(另一个休息调用在与更新产品和工作正常的部分几乎相同的代码上运行) . 我没有想法,但我的假设是查询是错误的,据说我仍然找不到究竟是什么错误。

这是我的一些代码:

控制器:

@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": []
}   

        ]
}

标签: mysqlspringrestspring-boot

解决方案


我认为您错过了注释中的value属性。@Query

将您的查询更改为:

@Query( value = "update Product p set p.amount = (p.amount - :amount) where p.id = :id")

推荐阅读