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
:
@RestResource
for Child
class -> Create all the instances of Child
before creating instances of Super
or Super2
-> Pass the URLs of all the Child
instances in payload of Super
or Super2
.Child
in the payload of Super
or Super2
without exposing @RestResource
for Child
class and the CascadeType.ALL
will take care of creating Child
instances.There are some pros with both the methods:
Child
objects to Super
or Super2
just by POST
ing the url of new Child
to http://<server>:<port>/super/1/children
. But I definitely loose the cascading functionality of database if I use this method.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. :)
There is a third solution that should fit for you:
You'll still can use /children
, but you will be able to retrieve children with super and to post it!
To do this, just change your Super (and Super2) class like this:
public class Super {
@Id
@GeneratedValue
private Long id;
@JoinTable
@OneToMany(cascade = CascadeType.ALL)
@RestResource(exported=false)
private List<Child> children;
...
}
Then you can POST on /supers
:
{
"children": [
{
"childMetadata": "inner"
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With