Search code examples
springjpajacksonjson-deserialization

Unable to parse Json to Object : JPA ManytoOne unidirectional


I am using spring data rest with repositories, db is mysql.

I have objects Parent and child. Relationship is Many to one .

child to Parent relationship is unidirectional. I do not have List of child obj inside Parent.

Parent{

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   protected Long id = null;

   @NotNull(message = "name of user should not be null")
   @Size(min=2, max=30)
   private String name;

}

Child{ 

    private String name;

    @ManyToOne
    private Parent parent;
}

I have an parent "1" and i want to refer it to newly creating child. The json input for new child "POST" req is

 {
   "name":"child name",
    "parent":{
        "id":1
     }  
  }

Need help in associating parent "1" in newly creating child.

Is there any changes required in json format? I tried with "parent_id" as well but still having error.


Solution

  • Since spring-data-rest uses HATEOAS. The following format worked.

    {
     "name":"testname1",
     "parent": "http://localhost:8080/parent/2"
    }
    

    Thanks to all who tried to answer.