Jsonbackreference and Jsonmanagedreference, bidirectional between two classes

Asked

Viewed 221 times

1

I have the following Dtos

public class UserDTO {

    private String name;

    @JsonManagedReference("phone")
    private UserPhoneDTO phone;

}

public class PhoneDTO {

    private String number;

    @JsonManagedReference("user")
    private UserPhoneDTO user;

}

public UserPhoneDTO {

    @JsonBackReference("user")
    private UserDTO userDTO;

    @JsonBackReference("phone")
    private PhoneDTO phoneDTO;

}

I have a problem, where if I convert the json to a Userdto, the Phonedto that is inside the Userphone becomes Null. If I convert the json to a Phonedto, the Userdto inside the Userphone becomes Null.

Is there any way I can get around this problem?

Note: There is the Annotation @Jsonidentityinfo(Generator=Objectidgenerator.Property Generator.class, Property="id"), which solves this problem, but when it is used, it creates an id (the name of the property that is defined in the parameter), which causes other problems when sending twice the same id to the server. So I dismissed that alternative.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.