S
S
ScRsa2016-07-04 10:27:52
Java
ScRsa, 2016-07-04 10:27:52

How to deserialize Entity when using @JsonIdentityReference(alwaysAsId=true)?

There are related objects Master - Detail

public class Master {
    private long id;
    @OneToMany(mappedBy = "detail")
    @JsonIgnore
    private Collection<Detail> detail;
}

public class Detail {
    private long id;
    @ManyToOne
    @JoinColumn(name = "master_id", referencedColumnName = "id", nullable = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Master master;
}

In order to not receive a bunch of references to the Master in the JSON response when receiving the Detail list, it is worth @JsonIdentityReference(alwaysAsId=true). As a result, in the response I get an ID instead of the Master object.
Everything works fine until you have to do PUT/POST. Master by ID is not deserialized back.
How to correct this matter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Alenkov, 2016-07-04
@Borz

Separate Entity entities and POJO entities into different classes and serialize/deserialize via Mapper classes. Although this will increase the number of classes, it will simplify the further support of the application - internal and external entities can "scatter" in content over time.
If this option is not suitable, then write your own deserializer , in which you get the desired entity by ID.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question