Spring Boot REST JPA JSON格式?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (403)

我正在使用Spring Boot和JPA编写REST服务。我在具有复合键的表中有一对多的映射。假设如果父表“订单”有2个组合键(订单ID,销售日期),并且“订单明细”具有多个具有3个组合键(订单ID,销售日期和物料ID)的订单项,我的请求对象是。我想知道是否可以不显式设置连接列属性并保留对象; 就像我想发送以下POST有效内容并将其映射到Order对象。此映射不会为OrderDetail的组合键salesDate和orderId设置值。由于这种持续失败,由于重复。

{
    "salesDate": "2016-02-12",
    "orderId": 12,
    "Description":"Sample order",
    "orderDetail":[{
      "itemId": 1231,
      "quantity": 2
    },
    {
      "itemId": 23342,
      "quantity": 1
    }]
}

Order.java:

@Entity
@Table(name = "ORDR")
@IdClass(OrderId.class)
public class Order implements Serializable {

    private static final long serialVersionUID = 2496620945369260577L;

    @Id
    @Column(name = "SLS_DT")
    private Date salesDate;

    @Id
    @Column(name = "ORDR_ID")
    private int orderId;

    @Column(name = "ORDR_DESC")
    private short orderDescription;

    @Column(name = "CUST_ID")
    private int customerId;

    @OneToMany(cascade= CascadeType.ALL, mappedBy = "order")
    private List<OrderDetail> orderItems;

    public Date getSalesDate() {
        return salesDate;
    }

    public void setSalesDate(Date salesDate) {
        this.salesDate = salesDate;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public short getOrderDescription() {
        return orderDescription;
    }

    public void setOrderDescription(short orderDescription) {
        this.orderDescription = orderDescription;
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
}

OrderId.java:

public class OrderId {

    private Date salesDate;
    private int orderId;

    public Date getSalesDate() {
        return salesDate;
    }
    public void setSalesDate(Date salesDate) {
        this.salesDate = salesDate;
    }
    public int getOrderId() {
        return orderId;
    }
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }   
}

OrderDetail.java

@Entity
@Table(name= "ORDR_DTL")
@IdClass(OrderDetail.class)
public class OrderDetail implements Serializable{

    private static final long serialVersionUID = 3603127094767197954L;

    @Id
    @Column(name = "SLS_DT")
    private Date salesDate;

    @Id
    @Column(name = "ORDR_ID")
    private int orderId;

    @Id
    @Column(name = "ITEM_ID")
    private int itemId;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumns({@JoinColumn(name = "SLS_DT", referencedColumnName = "SLS_DT", insertable = false, updatable = false),
            @JoinColumn(name = "ORDR_ID", referencedColumnName = "ORDR_ID", insertable = false, updatable = false)})
    private Order order;

    private BigDecimal quantity;

    public Date getSalesDate() {
        return salesDate;
    }

    public void setSalesDate(Date salesDate) {
        this.salesDate = salesDate;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public int getItemId() {
        return itemId;
    }

    public void setItemId(int itemId) {
        this.itemId = itemId;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}

OrderDetailId.java:

public class OrderDetailId implements Serializable {

    private static final long serialVersionUID = 2312835624801595602L;
    private Date salesDate;
    private int orderId;
    public Date getSalesDate() {
        return salesDate;
    }
    public void setSalesDate(Date salesDate) {
        this.salesDate = salesDate;
    }
    public int getOrderId() {
        return orderId;
    }
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }
    public int getItemId() {
        return itemId;
    }
    public void setItemId(int itemId) {
        this.itemId = itemId;
    }
    private int itemId;
}

如果我传递下面的JSON,它按预期工作。但我不想重复订单明细中的密钥,也不想将数据库结构公开给客户端。

{
    "salesDate": "2016-02-12",
    "orderId": 12,
    "Description": "Sample order",
    "orderDetail": [{
        "salesDate": "2016-02-12",
        "orderId": 12,
        "itemId": 1231,
        "quantity": 2
    }, {
        "salesDate": "2016-02-12",
        "orderId": 12,
        "itemId": 23342,
        "quantity": 1
    }]
}

我可以传递第一个json并将其转换为JPA实体。但我相信应该有一个更清晰的方法来实现它。JPA文档对此没有多大帮助。请帮我解决这个问题。

提问于
用户回答回答于

你可以尝试@JsonIgnoreorderId成员上使用注释OrderDetailId。这应该从JSON输出压制成员。

@JsonIgnore
private int orderId;

还有类注释@JsonIgnoreProperties({"memberName"})。你不必在类源代码的“内部”标记每个成员。

@JsonIgnoreProperties({"orderId"})
public class OrderDetail implements Serializable {
    ...

扫码关注云+社区

领取腾讯云代金券