Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8.6 NoSuchMethodException with Hibernate 5.2 #102

Closed
coladict opened this issue Jan 19, 2017 · 7 comments
Closed

2.8.6 NoSuchMethodException with Hibernate 5.2 #102

coladict opened this issue Jan 19, 2017 · 7 comments
Milestone

Comments

@coladict
Copy link

Missing method:

org.hibernate.resource.transaction.spi.TransactionCoordinator.isJta()

Here's a patch to fix it that is guaranteed to be future-compatible because it asks through JPA, not the Hibernate-only APIs.

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 56302b7..79866c5 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
@@ -463,12 +463,12 @@ public class PersistentCollectionSerializer
         
         public static boolean isJTA(Session session) {
             try {
-                Object transactionCoordinator = getTransactionCoordinatorMethod.invoke(session);
-                Object transactionCoordinatorBuilder = getTransactionCoordinatorBuilderMethod.invoke(transactionCoordinator);
-                return (boolean) isJtaMethod.invoke(transactionCoordinatorBuilder);
-            } catch (Exception e) {
-                // Should never happen
-                throw new RuntimeException(e);
+                EntityManager em = (EntityManager) session;
+                em.getTransaction();
+                return false;
+            } catch (IllegalStateException e) {
+                // EntityManager is required to throw an IllegalStateException if it's JTA-managed
+                return true;
             }
         }
     }

You can actually remove everything else from the SessionReader static class.

Here's the Javadoc comment from the JPA specifications.

    /**
     * Return the resource-level <code>EntityTransaction</code> object.
     * The <code>EntityTransaction</code> instance may be used serially to
     * begin and commit multiple transactions.
     * @return EntityTransaction instance
     * @throws IllegalStateException if invoked on a JTA
     *         entity manager
     */
    public EntityTransaction getTransaction();
@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 19, 2017

@coladict Thank you for reporting this, suggesting a fix.

One question: which specific version of Hibernate 5.2.x have this problem?
This is very puzzling as latest patches specifically attempted to resolve this problem.

@coladict
Copy link
Author

We are using Hibernate 5.2.2.Final on the project this happened with.

@coladict
Copy link
Author

coladict commented Jan 20, 2017

In case you're still unable to reproduce it, I should note how our ObjectMapper is initialized.

	SessionFactoryImplementor emf = (SessionFactoryImplementor) EMUtil.getFactory();
	mapper.registerModule(new Hibernate5Module(emf, emf)
			.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION)
			.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING));

@maffelbaffel
Copy link

maffelbaffel commented Jan 21, 2017

Using Hibernate 5.2.6.Final bring up a similar error for me:

Caused by: java.lang.NoSuchMethodException: org.hibernate.resource.transaction.spi.TransactionCoordinator.isJta()
	at java.lang.Class.getMethod(Class.java:1786) ~[?:1.8.0_111]
	at com.fasterxml.jackson.datatype.hibernate5.PersistentCollectionSerializer$SessionReader.<clinit>(PersistentCollectionSerializer.java:457) ~[jackson-datatype-hibernate5-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.datatype.hibernate5.PersistentCollectionSerializer.initializeCollection(PersistentCollectionSerializer.java:344) ~[jackson-datatype-hibernate5-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.datatype.hibernate5.PersistentCollectionSerializer.findLazyValue(PersistentCollectionSerializer.java:314) ~[jackson-datatype-hibernate5-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.datatype.hibernate5.PersistentCollectionSerializer.serialize(PersistentCollectionSerializer.java:253) ~[jackson-datatype-hibernate5-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1429) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:951) ~[jackson-databind-2.8.6.jar:2.8.6]
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:285) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	... 64 more

@ankurbhakta
Copy link

I am facing the same issue with 5.2.7.Final. Can we get a patch in the 2.8.7 version?

@cowtowncoder
Copy link
Member

Quick question: has anyone been able to verify that patch works for 5.0, 5.1 and 5.2 hibernate versions?

@cowtowncoder cowtowncoder added this to the 2.8.7 milestone Feb 8, 2017
@cowtowncoder
Copy link
Member

I don't have good way to verify how well this works, but it does pass 5.2 test with H5.2.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants