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:
- expose
@restresourcechildclass -> create instances ofchildbefore creating instances ofsuperorsuper2-> pass urls ofchildinstances in payload ofsuperorsuper2. - pass details of
childin payload ofsuperorsuper2without exposing@restresourcechildclass ,cascadetype.alltake care of creatingchildinstances.
there pros both methods:
- with option 1, ability add new
childobjectssuperorsuper2posting url of newchildhttp://<server>:<port>/super/1/children. loose cascading functionality of database if use method. - with option 2, cascading functionalities of database loose flexibility of adding new
childinstances.
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:
- 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
Post a Comment