dimanche 1 mars 2015

Hibernate mapping JSON values from POST request incorrectly as null

I'm using Spring JPA in conjunction with Hibernate to create a service that simply extracts JSON data from the body of a POST request, and writes that data to a mysql database.


I've implemented the my Profile repository as follows:



package oxi.repositories;

import oxi.models.*;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.*;

@RepositoryRestResource(collectionResourceRel="Profile", path="profile")
public interface ProfileRepository extends JpaRepository<Profile, Long>{

}


When i send my POST, some of the JSON parameter values are incorrectly showing up in the database as null. For example, when I POST the following JSON, I see firstname and lastname strings are being written to my db as null



{
"firstname":"Foo",
"lastname":"Barnacles",
"email":"dmoney@dollabills.org",
"country":"USA",
"city":"ballin",
"age":45
}


I verified the correct values were being picked up by my Apache server



mod_dumpio: dumpio_in (data-HEAP): {\r\n "firstname":"Foo",\r\n "lastname":"Barnacles",\r\n "email":"dmoney@dollabills.org",\r\n "country":"USA",\r\n "city":"ballin",\r\n "age":45\r\n}


However, I get the following bindings in catalina.out



sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [45]
sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [ballin]
sql.BasicBinder:81 - binding parameter [3] as [VARCHAR] - [USA]
sql.BasicBinder:81 - binding parameter [4] as [VARCHAR] - [dmoney@dollabills.org]
sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]
sql.BasicBinder:69 - binding parameter [6] as [VARCHAR] - [null]


I can't seem to find anything wrong with my Entity object



package oxi.models;

import javax.persistence.*;
import org.apache.log4j.Logger;

@Entity
public class Profile{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long user_id;
@Column(name="email")
private String email;
@Column(name="country")
private String country;
@Column(name="city")
private String city;
@Column(name="age")
private int age;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;

//Constructor
public Profile(){
}


//Setters
public void setFirstName(String firstname){
this.firstname = firstname;
}

public void setLastName(String lastname){
this.lastname = lastname;
}

public void setEmail(String email){
this.email = email;
}

public void setCountry(String country){
this.country = country;
}

public void setCity(String city){
this.city = city;
}

public void setAge(int age){
this.age = age;
}

//Getters
public String getFirstName(){
logger.trace("Set parameter firstname: " + this.firstname);
return this.firstname;
}

public String getLastName(){
logger.trace("Set parameter lastname: " + this.lastname);
return this.lastname;
}

public String getEmail(){
return this.email;
}

public String getCountry(){
return this.country;
}

public String getCity(){
return this.city;
}

public int setAge(){
return this.age;
}

}


And i THINK my db is correctly formed (respective to my Entity Object)



+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| user_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| firstname | tinytext | YES | | NULL | |
| lastname | tinytext | YES | | NULL | |
| email | tinytext | YES | | NULL | |
| country | tinytext | YES | | NULL | |
| city | tinytext | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+


Does anybody know what might cause this behavior. Any suggestions would be appreciated... I have a feeling this one may be staring me right in the face.


thanks in advanced


Aucun commentaire:

Enregistrer un commentaire