On Mon, May 11, 2020 at 9:52 AM Alex T <
nnm....@gmail.com> wrote:
>
> Hi everyone!
>
> When serializing entity with many references to another same entity, if that entity marked @JsonIdentityInfo, first occurence of entity serialized as full entity, other occurrences are serialized as id. Its work.
> I need to serialize entities with type information, so I use polymorphic type handler.
>
> I created test project:
https://github.com/alex-t0/deserialization-fail-example.git
>
> Please see this classes:
>
> User -
https://github.com/alex-t0/deserialization-fail-example/blob/master/src/main/java/deserialization/fail/example/User.java
> UserPair -
https://github.com/alex-t0/deserialization-fail-example/blob/master/src/main/java/deserialization/fail/example/UserPair.java
> MapperUtil -
https://github.com/alex-t0/deserialization-fail-example/blob/master/src/main/java/deserialization/fail/example/MapperUtil.java
> UserSerializationTest -
https://github.com/alex-t0/deserialization-fail-example/blob/master/src/test/java/deserialization/fail/example/UserSerializationTest.java
>
> Serialization works as I expect:
>
> [
> "deserialization.fail.example.UserPair",
> {
> "user1": [
> "deserialization.fail.example.User",
> {
> "id": [
> "java.lang.Long",
> 42
> ],
^^^^^^^^^
This is wrong. Id should be plain number (`42`) and suggests the most
likely problem.
It should never be wrapped in type information (in fact, not even
normal `long`/`Long` properties, being
one of small number of "natural" types).
Definition JsonIdentityInfo info is the problem in this case:
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id",
scope = User.class
)
... because you are trying to use an actual physical field (`private
long Id`), but this definition would
try to generate new value and assumes there is no "real" field.
For using fields, you'd need to use
`ObjectIdGenerators.PropertyGenerator` (but then id must be populated
by your code).
Alternatively you should either just remove the field (if not needed),
or, if needed for Hibernate or such,
add `@JsonIgnore` on it.
But assuming this Id value comes from DB (sequence?), the first option
is probably what you want.
Combination of Hibernate module and JSON Identity might cause other
issues as well, since unfortunately Hibernate module
is not maintained and its support for Proxy types seems to cause
problems with some Jackson features.
Another possibility just for testing might be to try to not register
that module, just to isolate problematic component.
I hope this helps,
-+ Tatu +-