Reputation: 153
I have two simple POJO models persisted with Hibernate and I am using Jackson to serialize and deserialize objects to JSON.
Here are my models TestCar and 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
:
{
"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 ?
Upvotes: 1
Views: 3909
Reputation: 3913
It seems you're not using the ObjectMapper with the module installed. Perhaps you should reassign the mapper variable? Not sure to what extent ObjectMapper is mutable or a builder.
Upvotes: 1