POSTing nested objects using spring data rest? -


i have started using spring-data-rest application. have following jpa entities:

@entity public class super {     @id     private long id;      @jointable     @onetomany(cascade = cascadetype.all)     private list<child> children; }  -----------------------------------------  @entity public class super2 {     @id     private long id;      @jointable     @onetomany(cascade = cascadetype.all)     private list<child> children; }  -----------------------------------------  @entity public class child {     @id     private long id;      @column     private string childmetadata; } 

i can think of 2 methods of saving new instances of super or super2:

  1. expose @restresource child class -> create instances of child before creating instances of super or super2 -> pass urls of child instances in payload of super or super2.
  2. pass details of child in payload of super or super2 without exposing @restresource child class , cascadetype.all take care of creating child instances.

there pros both methods:

  1. with option 1, ability add new child objects super or super2 posting url of new child http://<server>:<port>/super/1/children. loose cascading functionality of database if use method.
  2. with option 2, cascading functionalities of database loose flexibility of adding new child instances.

is there have totally missed? want way use cascading functionality of database without loosing flexibility of adding new children on fly.

thanks help. :)

there third solution should fit you:

  1. pass details of child in payload of super or super2 without exposing @restresource for attribute 'children' of super (and super2).

you'll still can use /children, able retrieve children super , post it!

to this, change super (and super2) class this:

public class super {     @id     @generatedvalue     private long id;      @jointable     @onetomany(cascade = cascadetype.all)     @restresource(exported=false)     private list<child> children;      ... } 

then can post on /supers:

{     "children": [         {             "childmetadata": "inner"         }     ] } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -