Spring When saving data into DB everything is null

Viewed 255

I am trying to save user input into Database but when I do everything is null. The picture below is the state of my database right now. The first line is hard coded information. enter image description here

I am taking user input from a HTML form. The code for that is below.

<form action="addResults" modelAttribute="results">

 <label for="ttn">TTN: </label><br>
 <input type="text" id="ttn" name="fname"><br>
<label for="f">Full Name:</label><br>
<input type="text" id="lname" name="lname">
<label for="email">Email:</label><br>
<input type="text" id="email" name="lname">
<label for="age">Age:</label><br>
<input type="text" id="age" name="lname">
<label for="gen">Gender:</label><br>
<input type="text" id="gen" name="lname">
<label for="add">Address:</label><br>
 <input type="text" id="add" name="lname">
<label for="post">Post Code:</label><br>
<input type="text" id="post" name="lname">
<label for="res">Result:</label><br>
<input type="text" id="res" name="lname">
<input type="submit" value="Submit">
This is the controller code
//Add new results
@RequestMapping(value = "/addResults", method = RequestMethod.GET)
public String newHotel(@Valid @ModelAttribute TestResults results, BindingResult result) {
    trRepo.save(results);
    return "start";
}}

And this is the Java Class where all the annotations are.

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String email;
private String fullname;
private int age;
private String gender;
private String Address;
private String PostCode;
private String TTN;
private String TestResult;
public TestResults() {
    super();
    // TODO Auto-generated constructor stub
}


public TestResults(String email, String fullname, int age, String gender, String address, String postCode,
        String tTN, String testResult) {
    this.email = email;
    this.fullname = fullname;
    this.age = age;
    this.gender = gender;
    this.Address = address;
    this.PostCode = postCode;
    this.TTN = tTN;
    this.TestResult = testResult;
}

So what I am trying to do is populate the database with the fields above with all the data from a user input but currently it is getting saved as null. Has anyone got any ideas about how to fix this.

1 Answers

Your entity doesn't have a @column annotation in order to map it to the columns present in database :

 @Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@column(name = "email")
private String email;
Related