I have recently started using spring-data-rest for my application. I have the 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 a
@RestResource
forChild
class -> Create all the instances ofChild
before creating instances ofSuper
orSuper2
-> Pass the URLs of all theChild
instances in payload ofSuper
orSuper2
. - Pass the details of
Child
in the payload ofSuper
orSuper2
without exposing@RestResource
forChild
class and theCascadeType.ALL
will take care of creatingChild
instances.
There are some pros with both the methods:
- With option 1, I get an ability to add new
Child
objects toSuper
orSuper2
just byPOST
ing the url of newChild
tohttp://<server>:<port>/super/1/children
. But I definitely loose the cascading functionality of database if I use this method. - With option 2, I get all the cascading functionalities of database but I loose the flexibility of adding new
Child
instances.
Is there something I have totally missed? I want a way to use the cascading functionality of database without loosing the flexibility of adding new children on the fly.
Thanks for help. :)