Serialize ArrayList using hypersistence-utils

99 Views Asked by At

I have an issue when trying to save ArrayList of POJO to Postgres dataBase.

I followed the approach from https://vladmihalcea.com/how-to-map-json-collections-using-jpa-and-hibernate/ , but no luck.

This is sample pom:

        <dependency>
            <groupId>io.hypersistence</groupId>
            <artifactId>hypersistence-utils-hibernate-55</artifactId>
            <version>3.6.1</version>
        </dependency>

This is sample entity model:

@AllArgsConstructor
@Builder
@Getter
@Setter
public class History implements Serializable {
    @JsonSetter(nulls = Nulls.AS_EMPTY)
    private String topic;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime time;
}


@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Entity
@Getter
@Setter
@TypeDef(name = "json", typeClass = JsonType.class)
public class AccountingOperationJournal extends EntityObject {
    @Builder.Default
    @Column(length = 4000, columnDefinition = "jsonb")
    @Type(type = "json")
    private List<History> history = new ArrayList<>();
}

This is error:

Caused by: java.lang.IllegalArgumentException: The given string value: {"topic":null,"time":"2023-09-01 17:53:30"} cannot be transformed to Json object
    ... 125 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<ru.sbrf.tfido.documentary.ga.core.model.eod.History>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{"topic":null,"time":"2023-09-01 17:53:30"}"; line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1462)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:392)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:252)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
    at io.hypersistence.utils.hibernate.type.util.ObjectMapperWrapper.fromString(ObjectMapperWrapper.java:86)
    ... 124 more

The problem is that when there is only one value in the list, it is not serialized as an array. The string haven't got any []. I know that with jackson it is possible to fix by

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)

but I do not understand what I have to do with hypersistence

0

There are 0 best solutions below