如何使用Jackson和Hibernate序列化和反序列化Set属性

时间:2016-11-23 19:49:54

标签: java json hibernate serialization

我有两个简单的POJO模型与Hibernate持久化,我使用Jackson将对象序列化和反序列化为JSON。

以下是我的模型TestCar和TestPart:

public class TestCar
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id; 

    private String name;

    @OneToMany(targetEntity = TestPart.class, mappedBy = "car", cascade = { CascadeType.PERSIST }, orphanRemoval=true, fetch = FetchType.LAZY)
    private Set<TestPart> parts = new HashSet<>();
}

public class TestPart
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id; 

    private String name;

    @JsonIgnore
    @ManyToOne
    private TestCar car;
}

我的杰克逊初始化:

Hibernate4Module h4m = new Hibernate4Module();
h4m.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING);

mapper = new ObjectMapper();
mapper.registerModule(h4m);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.enableDefaultTyping();

private Set<TestPart> pars存在问题,因为它被序列化为org.hibernate.collection.internal.PersistentSet

{
    "id" : 1,
    "name" : "Fiat",
    "parts" : [ "org.hibernate.collection.internal.PersistentSet", [ [ "com.acme.TestPart", {
            "id" : 1,
            "name" : "puerta"
      } ]
     ] ]
}

当我尝试反序列化它时会抛出错误:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserializeWithType(CollectionDeserializer.java:320)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:497)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:101)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)

有没有办法将hibernate PersistentSet序列化为一个简单的Set?

1 个答案:

答案 0 :(得分:0)

您似乎未在安装模块的情况下使用ObjectMapper。也许你应该重新分配mapper变量?不确定ObjectMapper在多大程度上是可变的或构建器。