FasterXML / FasterXML/jackson-datatype-hibernate

2.8.7 NoSuchMethodError PersistentCollection.setCurrentSession

Open
#104 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
331
Forks
108
Avg merge
5h 3m
Merged PRs (30d)
1

Description

They changed the signature of the method in 5.2 and now instead of `SessionImplementor` it takes `SharedSessionContractImplementor`.

This fixed it.
```diff
diff --git a/hibernate5/src/main/java/com/fasterxml/jackson/datatype/hibernate5/PersistentCollectionSerializer.java b/hibernate5/src/main/java/com/fasterxml/jackson/datatype/hibernate5/PersistentCollectionSerializer.java
index 140d8b3..392eb31 100644
--- a/hibernate5/src/main/java/com/fasterxml/jackson/datatype/hibernate5/PersistentCollectionSerializer.java
+++ b/hibernate5/src/main/java/com/fasterxml/jackson/datatype/hibernate5/PersistentCollectionSerializer.java
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
import com.fasterxml.jackson.databind.util.NameTransformer;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
+import java.lang.reflect.InvocationTargetException;

import org.hibernate.FlushMode;
import org.hibernate.Hibernate;
@@ -63,6 +64,8 @@ public class PersistentCollectionSerializer

protected final SessionFactory _sessionFactory;

+ protected final Method _collectionSessionInitMethod;
+
/*
/**********************************************************************
/* Life cycle
@@ -77,6 +80,7 @@ public class PersistentCollectionSerializer
_serializer = (JsonSerializer) serializer;
_features = features;
_sessionFactory = sessionFactory;
+ _collectionSessionInitMethod = initCollMetod();
}

/**
@@ -90,6 +94,22 @@ public class PersistentCollectionSerializer
_serializer = (JsonSerializer) serializer;
_features = base._features;
_sessionFactory = base._sessionFactory;
+ _collectionSessionInitMethod = initCollMetod();
+ }
+
+ protected Method initCollMetod() {
+ try {
+ Class cl = SessionFactory.class.getClassLoader().loadClass("org.hibernate.collection.spi.PersistentCollection");
+ for (Method m : cl.getMethods()) {
+ if (m.getName().equals("setCurrentSession")) {
+ return m;
+ }
+ }
+ }
+ catch (ClassNotFoundException ex) {
+ throw new IllegalStateException("Hibernate version is incompatible for lazy collection serialization", ex);
+ }
+ throw new IllegalStateException("Hibernate version is incompatible for lazy collection serialization");
}

@Override
@@ -348,7 +368,13 @@ public class PersistentCollectionSerializer
session.beginTransaction();
}

- coll.setCurrentSession(((SessionImplementor) session));
+ try {
+ _collectionSessionInitMethod.invoke(coll, session);
+ }
+ catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+ throw new IllegalStateException("Hibernate version is incompatible for lazy collection serialization", ex);
+ }
+
Hibernate.initialize(coll);

if (!isJTA) {
```

If you migrated to Gradle you could actually test different provider versions in the same build.
For example, I managed to do it like this https://github.com/mopano/hibernate-json-type/blob/d25edecea9cab619207e4888c850c96bc65f10ea/build.gradle

Lines 28-32 define configurations for each provider;
Lines 50-52 define dependency for each configuration;
Lines 62-81 define new test tasks for each provider configuration.
Lines 66, 73, 80 set classpaths for each task accordingly.

That's the kind of flexibility Maven will never provide.

With this patch it managed to serialize my object that was crashing after it had just been persisted.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.