Update: I've managed to fix some minor errors and at this stage, the parent object (Entry) gets saved, but the child (Address) does not.
-----------------------------------------------
Hello,
I've implemented bidirectional @OneToMany mapping. When done, I'm sending the parent object to the RESTful api.
So the entities are on the API side. On client side I'm using DTO objects that of course very much resemble the entity API classes.
I had to annotate as in the following code snippet the DTOs to avoid an exception saying something about inifinite recursion.
Code:
public class EntryDto {
private Long id;
@NotNull
private String name;
@JsonManagedReference
private List<AddressDto> addresses = new ArrayList<>();
.....
public class AddressDto {
private Long id;
@NotNull
private String line1;
@Null
private String line2;
@NotNull
@JsonBackReference
private EntryDto entryDto;
public AddressDto(String line1, String line2){
this.line1 = line1;
this.line2 = line2;
}
public AddressDto(String line1, EntryDto entryId){
this.line1 = line1;
this.entryDto = entryId;
}
The exception is gone when I annotated like above.
However, when I debug my API app, I notice that the objects are still inifinitely nested.
When I set the breakpoing on the line where I'm saving object Entry entry, I can see that:
entry
id = null
name = "name i gave it"
addressess
0 // the single assigned address
id = null
line1 = "value I set for line 1"
line2 = "value I set for line 2"
entry
entry
id = null
name = "name i gave it"
addressess
0 // the single assigned address
id = null
line1 = "value I set for line 1"
line2 = "value I set for line 2"
entry
... and so on
How do I deal with this issue?
------------------------------------------------
Update: I've managed to fix some minor errors and at this stage, the parent object (Entry) gets saved, but the child (Address) does not.