Sorry to jump into the conversation, but wasn't it what he has been doing in his User class already? (using ObjectIdGenerators.PropertyGenerator in his JsonIdentityInfo definition)
Thanks for any explanation. maxxyme On Sat, May 16, 2020, 06:07 Tatu Saloranta <[email protected]> wrote: > On Thu, May 14, 2020 at 7:38 AM Alex T <[email protected]> wrote: > >> Hi Tatu! >> >> For now I solve issue changing from mapper.activateDefaultTyping(ptv, >> ObjectMapper.DefaultTyping.EVERYTHING); to >> mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); >> >> Please see >> https://github.com/alex-t0/deserialization-fail-example/commit/5c1e9f73a21567c04823145993ffd09c314a276e#diff-590456d68d9f15ce57513829910aa370 >> . >> >> *Now it does not wrap user id. And deserialization works fine.* >> > > That makes sense. Actually having Type Id for `Long` is fine in itself, > but it would not have worked as type id most likely. And there can not be > any polymorphism anyway in that location. > > But I'm not understand what you mean about @JsonIgnore'ing or removing my >> id field. It is a primary key and I need this field. If I add @JsonIgnore >> to id field I get this error: >> > > What I mean is that combination of settings you had was incompatible. You > need to either: > > 1. Let Jackson generate Object Id by itself and NOT have id field in your > POJO, OR > 2. Use an existing field as Object Id and make sure Jackson uses that > field and does NOT generate Object id. > > For second, I already said that... > > "For using fields, you'd need to use > `ObjectIdGenerators.PropertyGenerator`" > > so change generator type to that, from what you had. > > -+ Tatu +- > > > >> >> Caused by: java.lang.IllegalArgumentException: Invalid Object Id >> definition for deserialization.fail.example.User: cannot find property with >> name 'id' >> at >> com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructObjectIdHandler(BeanSerializerFactory.java:491) >> at >> com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanOrAddOnSerializer(BeanSerializerFactory.java:414) >> at >> com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanOrAddOnSerializer(BeanSerializerFactory.java:286) >> at >> com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:231) >> at >> com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:165) >> at >> com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:1474) >> at >> com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1422) >> ... 100 more >> >> PS. Can you reemove my second duplicate thread >> https://groups.google.com/forum/#!topic/jackson-user/MK8RLm8Unu0? >> >> On Tuesday, May 12, 2020 at 2:07:55 AM UTC+3, Tatu Saloranta wrote: >>> >>> On Mon, May 11, 2020 at 9:52 AM Alex T <[email protected]> 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 +- >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "jackson-user" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/jackson-user/52f9385a-3185-439d-a1f0-c1f881587bfc%40googlegroups.com >> <https://groups.google.com/d/msgid/jackson-user/52f9385a-3185-439d-a1f0-c1f881587bfc%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "jackson-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jackson-user/CAGrxA27a8GjJ6mgybdJTYcNqYpbJ38%3Daa7uYgOrgBVtWBD75MA%40mail.gmail.com > <https://groups.google.com/d/msgid/jackson-user/CAGrxA27a8GjJ6mgybdJTYcNqYpbJ38%3Daa7uYgOrgBVtWBD75MA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAEEu3mBTO7X0PomkYEfAWiXGsFxchuHu3kpX%3Dy8BjiCxSZJ71A%40mail.gmail.com.
