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
@restresource
child
class -> create instances ofchild
before creating instances ofsuper
orsuper2
-> pass urls ofchild
instances in payload ofsuper
orsuper2
. - pass details of
child
in payload ofsuper
orsuper2
without exposing@restresource
child
class ,cascadetype.all
take care of creatingchild
instances.
there pros both methods:
- with option 1, ability add new
child
objectssuper
orsuper2
post
ing url of newchild
http://<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
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:
- 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