0

JPA/Spring returning JSON from 2 MySQL tables as nested Objects

I'm brand new to JPA/Spring so if you could atleast point me in the right direction it would be extremely helpful!! Cheers!

Here is what I get:

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

What I want is:

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

Java Class 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(){

}
}

tblPeople:

@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() {
}
1
  • On a side node: naming your classes as if they were tables is really bad form. You're doing object oriented programming here, there should be no mention of tables or foreign keys (except maybe in mapping annotations). Let JPA take care of that. Commented Jun 2, 2015 at 18:40

2 Answers 2

1

Instead of this

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

you need this class structure (to get the JSON you want)

 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;
    }

It means, you need to create a separate class structure with all attributes inside Single class to get the JSON you want. And it has nothing to do with JPA.

Sign up to request clarification or add additional context in comments.

Comments

0

Change Class to:-

    @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")
    @JsonIgnore
    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 getFirstName(){
        return tblPeople.getFirstName();
        }
       // do the samething for rest of  variables
    public TblPeopleContactInfo(){

        }
     }

2 Comments

Wow this is excellent! Now the Json is alot easier to call! I really appreciate it!
Is it ok to use setter like this? public void setNotes(String notes) { tblPeople.setNotes(notes); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.