Home > Mobile >  Data being entered as NULL in database through Spring Application
Data being entered as NULL in database through Spring Application

Time:06-01

I am trying to enter data into my database, but some fields are being entered as NULL

Here is fields of my Model class on Spring

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String AdType;
private String AdTitle;
private String ImageUrl;
private String LandingUrl;
private String Placement;
private String Country;
private String DSP;
// .. getters and setters

React code that is sending the request:

const handleClick = (e) => {
    e.preventDefault()
    const creative = {ad_title, image_url, landing_url, ad_type, placement, country, dsp}
    fetch("http://localhost:8080/api/auth/creative/add", {
        method: "POST",
        headers: {"Content-Type":"application/json"},
        body:JSON.stringify(creative)
    }).then(() => {
        console.log("New creative added")
    })
}

The values of placement, country, and DSP are being entered correctly but the rest are being entered as NULL. I personally think it might be because of them having an '_' between their names. enter image description here

CodePudding user response:

It looks like a mapping problem caused by the underscore. Try changing the attribute names of your model class to camelCase. Maybe that solves your problem.

So it looks somewhat like this: AdType -> adType and so on...

CodePudding user response:

The way I solved this was by renaming the fields of my Model class in the application. I basically changed AdType to ad_type in order to match the column names in the database.

  • Related