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
@RestResourceforChildclass -> Create all the instances ofChildbefore creating instances ofSuperorSuper2-> Pass the URLs of all theChildinstances in payload ofSuperorSuper2. - Pass the details of
Childin the payload ofSuperorSuper2without exposing@RestResourceforChildclass and theCascadeType.ALLwill take care of creatingChildinstances.
There are some pros with both the methods:
- With option 1, I get an ability to add new
Childobjects toSuperorSuper2just byPOSTing the url of newChildtohttp://<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
Childinstances.
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. :)