gpt4 book ai didi

java - JPA/Spring 从 2 个 MySQL 表返回 JSON 作为嵌套对象

转载 作者:行者123 更新时间:2023-11-29 12:11:27 25 4
gpt4 key购买 nike

JPA/Spring 从 2 个 MySQL 表返回 JSON 作为嵌套对象

我是 JPA/Spring 的新手,所以如果你至少能指出我正确的方向,那将会非常有帮助!干杯!

这是我得到的:

[{"conId":1,"phone1":"test","tblPeople":   {"pepId":1,"pepAccountIdFk":1,"firstName":"TestFirst","lastName":"Last","title":"Title","notes":"Notes"}}]

我想要的是:

 [{"conId":1,"phone1":"test",  "pepId":1,"pepAccountIdFk":1,"firstName":"TestFirst","lastName":"Last","title":"Title","notes":"Notes"}]

Java 类 tblPeopleContactInfo:

package CMS;




@Entity
public class TblPeopleContactInfo implements Serializable{

private static final long serialVersionUID = 1L;

@Id
private int conId;

@Column(name="con_phone_1")
private String phone1;



@OneToOne(optional=false)
@JoinColumn( name="con_person_id_fk")
private TblPeople tblPeople;


public String getphone1() {
return phone1;
}

public void setphone1(String phone1) {
this.phone1 = phone1;
}

public TblPeople getTblPeople() {
return tblPeople;
}

public void setTblPeople(TblPeople tblPeople) {
this.tblPeople = tblPeople;
}

public int getConId() {
return conId;
}

public void setConId(int conId) {
this.conId = conId;
}




public TblPeopleContactInfo(){

}
}

tbl人:

@Entity
public class TblPeople{
@Id
private int pepId;
private int pepAccountIdFk;
@Column(name="pep_first_name")
private String firstName;
@Column(name="pep_last_name")
private String lastName;
@Column(name="pep_title")
private String title;
@Column(name="pep_notes")
private String notes;

@OneToOne(mappedBy="tblPeople")
@JsonIgnore
private TblPeopleContactInfo tblPeopleContactInfo;


public TblPeopleContactInfo getTblPeopleContactInfo() {
return tblPeopleContactInfo;
}


public void setTblPeopleContactInfo(TblPeopleContactInfo tblPeopleContactInfo) {
this.tblPeopleContactInfo = tblPeopleContactInfo;
}







public int getPepId() {
return pepId;
}


public void setPepId(int pepId) {
this.pepId = pepId;
}


public int getPepAccountIdFk() {
return pepAccountIdFk;
}


public void setPepAccountIdFk(int pepAccountIdFk) {
this.pepAccountIdFk = pepAccountIdFk;
}


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 getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public String getNotes() {
return notes;
}


public void setNotes(String notes) {
this.notes = notes;
}


public TblPeople() {
}

最佳答案

而不是这个

public class TblPeopleContactInfo implements Serializable{
private int conId;
private String phone1;
private TblPeople tblPeople;
}

你需要这个类结构(以获取你想要的 JSON)

 public class TblPeopleContactInfo implements Serializable{
private int conId;
private String phone1;
private int pepId;
private int pepAccountIdFk;
private String firstName;
private String lastName;
private String title;
private String notes;
}

这意味着,您需要创建一个单独的类结构,并将所有属性包含在 Single 类中以获得您想要的 JSON。而且和JPA没有任何关系。

关于java - JPA/Spring 从 2 个 MySQL 表返回 JSON 作为嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30603817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com