0

我正在使用ImmutablesHibernate Types将对象序列jsonb化为 PostgreSQL。

我的实体映射如下:

@Entity
@Table(schema = "data", name = "event")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Event {

    @Id
    @Column(name = "id")
    private String id;

    @Type(type = "jsonb")
    @Column(name = "payload")
    private Aggregate payload;

}

Aggregate看起来像这样:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "aggregateType",
        defaultImpl = GenericAggregate.class,
        visible = true
)
@JsonSubTypes({
        @Type(ConcreteAggregate.class)
})
public interface Aggregate {
}

我的具体类型为Aggregate

@Value.Immutable
@JsonTypeName("concrete-aggregate")
@JsonSerialize(as = ImmutableConcreteAggregate.class)
@JsonDeserialize(as = ImmutableConcreteAggregate.class)
public interface ConcreteAggregate extends Aggregate {
    // fields
}

从数据库读取时出现以下异常:

java.lang.IllegalArgumentException: Class com.example.ConcreteAggregate not subtype of [simple type, class com.example.ConcreteAggregateBuilder$ImmutableConcreteAggregate]

    at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:357)
    at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:191)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:113)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1178)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper.fromString(ObjectMapperWrapper.java:42)
    at com.vladmihalcea.hibernate.type.util.ObjectMapperJsonSerializer.clone(ObjectMapperJsonSerializer.java:22)
    at com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper.clone(ObjectMapperWrapper.java:73)
    at com.vladmihalcea.hibernate.type.json.internal.JsonTypeDescriptor$2.deepCopyNotNull(JsonTypeDescriptor.java:39)

我该如何解决这个问题?

4

1 回答 1

1

TL;博士

让您的 JSON DTO(Aggregate本例中的接口)实现java.io.Serializable.

为什么?

发生此异常是因为如果您的类未实现,Hibernate Types 会尝试com.vladmihalcea.hibernate.type.util.ObjectMapperJsonSerializer使用 Jackson克隆对象。所以这只是让你的 JSON DTO 实现的问题,它应该可以工作。ObjectMapperjava.io.Serializablejava.io.Serializable

于 2018-12-04T10:48:52.023 回答