【问题标题】:How to send values to parent table column in json如何将值发送到json中的父表列
【发布时间】:2020-03-19 20:17:03
【问题描述】:

客户实体

@Entity
@Table(name="Customer")
public class Customer {

    @Id
    @GeneratedValue
    private Long cusId;
    @Column(name="firstname")
    private String firstName;

    @Column(name="lastname")
    private String lastName;


    @Column(name="address")
    private String add;


    @Column(name="tel")
    private int tel;


    @Column(name="email")
    private String email;

    @Column(name="userName")
    private String username;

    @Column(name="password")
    private String password;

    @OneToMany(fetch = FetchType.EAGER,  mappedBy = "customer")
    @JsonManagedReference
    private List<Complain> complain=new ArrayList<Complain>();
    public Customer() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Long getCusId() {
        return cusId;
    }
    public void setCusId(Long cusId) {
        this.cusId = cusId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getAdd() {
        return add;
    }
    public void setAdd(String add) {
        this.add = add;
    }
    public int getTel() {
        return tel;
    }
    public void setTel(int tel) {
        this.tel = tel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public List<Complain> getComplain() {
        return complain;
    }
    public void setComplain(List<Complain> complain) {
        this.complain = complain;
    }

}

投诉实体

@Entity
@Table(name="Complain")

public class Complain {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name="repfirst")
    private String repfirst;

    @Column(name="replast")
    private String replast;


    @Column(name="warranty")
    private String warranty;

    @Column(name="dop")
    private String purDate;

    @Column(name="Nomachine")
    private String Nomachine;

    @Column(name="Complain")
    private String Complain;

    @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name="cus_id", referencedColumnName = "cusId")
    @JsonBackReference
    private Customer customer;


    public Complain() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRepfirst() {
        return repfirst;
    }

    public void setRepfirst(String repfirst) {
        this.repfirst = repfirst;
    }

    public String getReplast() {
        return replast;
    }

    public void setReplast(String replast) {
        this.replast = replast;
    }

    public String getWarranty() {
        return warranty;
    }

    public void setWarranty(String warranty) {
        this.warranty = warranty;
    }

    public String getPurDate() {
        return purDate;
    }

    public void setPurDate(String purDate) {
        this.purDate = purDate;
    }

    public String getNomachine() {
        return Nomachine;
    }

    public void setNomachine(String nomachine) {
        Nomachine = nomachine;
    }

    public String getComplain() {
        return Complain;
    }

    public void setComplain(String complain) {
        Complain = complain;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    }

我想通过postman工具把数据传给mysql数据库。如何通过json查询传递外键列的值?

这是我在控制器中使用的方法

@PostMapping(path="/",consumes="application/json",produces="application/json")
public void addComplain(@RequestBody Complain complain)
{               


    Integer id = complainService.getAllComplain().size()+1;
    complain.setId(new Long(id));

    complainService.createOrUpdateComplain(complain);   }

我从邮递员发出请求后,除了外键之外的所有数据都保存了下来。我想我在映射两个表时遗漏了一些东西。

这是我的 json 查询

{"complain":"No power",
 "repfirst":"hi",
 "replast":"all",
 "warranty":"yes",
 "purDate":"2020-02-29",
 "nomachine":"6",
 "tel":"46544654",
 "Customer":[
             {"cusId":"1"}
            ]
}

Database table image

请帮忙,提前谢谢

【问题讨论】:

    标签: json spring rest api


    【解决方案1】:
    {
     "repfirst": "",
     "replast": "",
     "warranty": "",
     "purDate" : "",
     "Nomachine": "",
     "Complain": "",
     "cus_id": 1
    }
    

    应该可以的

    "Customer":[
                 {"cusId":"1"}
                ]
    

    例如,如果您将 Set&lt;Customer&gt; customers 转换为 json 中的数组,这将起作用。

    【讨论】:

    • 我尝试了您建议的两种方式。但我有同样的问题。 CusID 列未收到数据。
    猜你喜欢
    • 2014-09-06
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2019-01-13
    • 2013-12-29
    • 2023-03-05
    相关资源
    最近更新 更多