简体   繁体   English

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

[英]How to serialize and deserialize a Set attribute with Jackson and Hibernate

I have two simple POJO models persisted with Hibernate and I am using Jackson to serialize and deserialize objects to JSON. 我有两个简单的POJO模型与Hibernate一起使用,并且我正在使用Jackson来将对象序列化和反序列化为JSON。

Here are my models TestCar and TestPart: 这是我的模型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;
}

My Jackson initialization: 我的杰克逊初始化:

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();

There is an issue with the private Set<TestPart> pars because it is serialized as org.hibernate.collection.internal.PersistentSet : 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"
      } ]
     ] ]
}

and when I try to deserialize it an error is thrown: 当我尝试反序列化时,会引发错误:

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)

Is there any way to serialize the hibernate PersistentSet as a simple Set ? 有什么方法可以将休眠的PersistentSet序列化为一个简单的Set?

It seems you're not using the ObjectMapper with the module installed. 看来您没有在安装模块的情况下使用ObjectMapper。 Perhaps you should reassign the mapper variable? 也许您应该重新分配mapper变量? Not sure to what extent ObjectMapper is mutable or a builder. 不知道ObjectMapper在什么程度上是可变的或生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM