在Spring Boot Rest Controller中更新OneToMany中的字段无法正常工作

时间:2017-05-17 22:36:42

标签: hibernate spring-boot hibernate-mapping spring-restcontroller

我有这个表格关系One PurchaseOrder to Many PurchaseOrderProducts

PurchaseOrder实体: -

    @Entity
    public class PurchaseOrder {

        @Id
        @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
        @GeneratedValue(generator = "uuid")
        @Column(name = "purchase_order_id")
        @Type(type = "uuid-char")
        private UUID purchaseOrderId;

        @Size(min = 2, max = 20)
        @Column(name = "order_no")
        private String orderNo;

        private Date orderDate = new Date();

        @OneToMany(mappedBy = "purchaseOrder", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
        @JsonManagedReference
        private Set<PurchaseOrderProducts> purchaseOrdersProducts = new HashSet<PurchaseOrderProducts>();

    //...

public Set<PurchaseOrderProducts> getPurchaseOrdersProducts() {
        return purchaseOrdersProducts;
    }

    public void setPurchaseOrdersProducts(Set<PurchaseOrderProducts> purchaseOrdersProducts) {
        this.purchaseOrdersProducts.clear();
        for (PurchaseOrderProducts poProduct : purchaseOrdersProducts) {
            poProduct.setPurchaseOrder(this);
            this.purchaseOrdersProducts.add(poProduct);
        }
    }
//...
@Override
public String toString() {
    String result = String.format("PurchaseOrder [id='%s', orderNo='%s']%n", purchaseOrderId, orderNo);
    if (purchaseOrdersProducts != null) {
        for (PurchaseOrderProducts purchOrdrProduct : getPurchaseOrdersProducts()) {
            result += String.format("Purchase Products[Product Name='%s', qty=%d]%n",
                    purchOrdrProduct.getProduct().getProductName(), purchOrdrProduct.getPurchaseQty());
        }
    }
    return result;
}
    }

PurchaseOrderProducts实体: -

    @Entity
    public class PurchaseOrderProducts implements Serializable {

        @Id
        @ManyToOne
        @JoinColumn(name = "product_id",referencedColumnName = "product_id")
        private ProductMaster product;

        @Id
        @ManyToOne(fetch = FetchType.LAZY)
        @JsonBackReference
        @JoinColumn(name = "purchase_order_id",referencedColumnName = "purchase_order_id")
        private PurchaseOrder purchaseOrder;

         @NotNull
         private Integer purchaseQty;
    //...
    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(purchaseOrder);
        hcb.append(product);
        return hcb.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof PurchaseOrderProducts)) {
            return false;
        }
        PurchaseOrderProducts that = (PurchaseOrderProducts) obj;
        EqualsBuilder eb = new EqualsBuilder();
        eb.append(purchaseOrder, that.purchaseOrder);
        eb.append(product, that.product);
        return eb.isEquals();
    }
//...
    }

要更新的控制器代码: -

@RestController
@RequestMapping(value = "/rest/purchaseOrder")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    //...
@RequestMapping(value="update",method = RequestMethod.PUT)
    public PurchaseOrder updatePurchaseOrder(@RequestBody PurchaseOrder purchaseOrderObj){

            return purchaseOrderService.save(purchaseOrderObj);
    }
}

使用以下JSON保存新记录非常有效: -

{
  "purchaseOrderId": null,
  "orderNo": "PO0001-0517",
  "orderDate": 1495057510000,
  "purchaseOrdersProducts": [
    {
      "product": {
        "productId": 1,
        "productName": "PRODUCT 1"
      },
      "purchaseQty": 10,
    },
    {
      "product": {
        "productId": 2,
        "productName": "PRODUCT 2"
      },
      "purchaseQty": 20,
    }
   ]
}

但是在使用一个额外的purchaseOrderProduct记录(下面的JSON中显示的产品3)保存后更新相同的记录不起作用: -

{
      "purchaseOrderId": "32c1eb58-ebd5-49a9-82e5-659b7f8a1063",
      "orderNo": "PO0001-0517",
      "orderDate": 1495057510000,
      "purchaseOrdersProducts": [
        {
          "product": {
            "productId": 1,
            "productName": "PRODUCT 1"
          },
          "purchaseQty": 10,
        },
        {
          "product": {
            "productId": 2,
            "productName": "PRODUCT 2"
          },
          "purchaseQty": 20,
        },
        {
          "product": {
            "productId": 3,
            "productName": "PRODUCT 3"
          },
          "purchaseQty": 25,
        }
       ]
    }

此外,如果我从JSON中删除一个purchaseOrdersProducts并尝试更新它不起作用。没有抛出错误但是当我检查表数据没有改变时

控制台日志: -

insert into purchase_order_products (purchase_order_id, product_id,purchase_qty ..) values (?, ..)
o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'purchase_order_id' cannot be null

请指导正确的方法。我已在实体字段中尝试了许多组合。

0 个答案:

没有答案