0

I have to use insert as well as retrieve operation for the below object from spring boot rest API. 1) while insertion I am getting NullPointerException at nested Address json object, at back-end I have created two tables and joined them with addredd_id as a foreign key. While using JPA i am able to insert. 2) with the same Entities I have to retrieve this complex object as response too, But I am getting only company details data not address data. I have also applied @OneToOne relationship with cascade type ALL. Please let me know where I am doing wrong.

My JSON Object to insert.

{ 
        "ct_company_id"                       : "",
        "ct_company_owner_name"               : "oname",
        "ct_company_name"                     : "cname",
        "ct_company_address"                  : [ 
            { "ct_address_id"   : "" ,
              "ct_address_1"    : "add1" ,
              "ct_address_2"    : "add2" ,
              "ct_address_3"    : "add3" ,
              "ct_country"      : "country" ,
              "ct_state"        : "state" ,
              "ct_town_n_city"  : "city" ,
              "ct_postal_code"  : "123456" 
            }
        ] ,
        "ct_company_email_id"                 : "email@gmail.com",
        "ct_company_mobile_no"                : "987654321",
        "ct_company_phone_no"                 : "987654321",
        "ct_company_gst_no"                   : "GST1234567",
        "ct_company_pan_no"                   : "PAN1234567",
        "ct_company_website"                  : "http://www.example.com",
        "ct_company_sac_code"                 : "SAC1234567",
        "ct_company_logo_name"                : "",
        "ct_company_digital_sign_stamp_name"  : "",
        "ct_company_term_condition"           : "Test",
        "ct_company_revoke"                   : "N",
        "ct_company_status"                   : "status",
        "ct_company_last_update_datetime"     : "2017-11-21T07:02:49.000Z",
        "ct_company_last_update_ip"           : "123.123.1.1",
        "ct_company_last_update_login_id"     : "login"
    }

Below are the entities.

@Entity
@Table(name = "ct_company_demo")
public class CTCompanyDetailsEO implements Serializable {
    private static final long serialVersionUID = -469014552293726485L;
    private int ct_address_id;
    private String ct_company_id;
    private String ct_company_owner_name;
    private String ct_company_name;
    private CTAddressDetailsEO ctAddressDetailsEO;
    private String ct_company_email_id;
    private String ct_company_mobile_no;
    private String ct_company_phone_no;
    private String ct_company_gst_no;
    private String ct_company_pan_no;
    private String ct_company_website;
    private String ct_company_sac_code;
    private String ct_company_logo_name;
    private byte[] ct_company_logo;
    private String ct_company_digital_sign_stamp_name;
    private byte[] ct_company_digital_sign_stamp;
    private String ct_company_term_condition;
    private char ct_company_revoke;
    private String ct_company_status;
    private Date ct_company_last_update_datetime;
    private String ct_company_last_update_ip;
    private String ct_company_last_update_login_id;

    //I have joined with the foreign key here.
    //Getters and Setters
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "ct_address_id")
    @JsonBackReference
    public CTAddressDetailsEO getCtAddressDetailsEO() {
        return ctAddressDetailsEO;
    }
    public void setCtAddressDetailsEO(CTAddressDetailsEO ctAddressDetailsEO) 
      {
        this.ctAddressDetailsEO = ctAddressDetailsEO;
    } 
}
@Entity
@Table(name = "ct_address_details")
public class CTAddressDetailsEO implements Serializable {
    private static final long serialVersionUID = -4481022081360378745L;
    private int ct_address_id;
    private String ct_address_1;
    private String ct_address_2;
    private String ct_address_3;
    private String ct_country;
    private String ct_state;
    private String ct_town_n_city;
    private String ct_postal_code;
    private CTCompanyDetailsEO ctCompanyDetailsEO;
    //I have added mappedBy here for Bidirectional mapping.
    // Getters and setters
    @OneToOne(mappedBy = "ctAddressDetailsEO")
    public CTCompanyDetailsEO getCtCompanyDetailsEO() {
        return ctCompanyDetailsEO;
    }
    public void setCtCompanyDetailsEO(CTCompanyDetailsEO ctCompanyDetailsEO) {
        this.ctCompanyDetailsEO = ctCompanyDetailsEO;
    }
}   
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

0 Answers0