Skip to content

Commit 2cac852

Browse files
committed
Fixes for PersistentCollectionSerializer
1 parent c5dc9d0 commit 2cac852

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.datatype.hibernate5;
2+
3+
public class Hibernate5Version {
4+
5+
public static String getHibernateVersion(){
6+
try {
7+
return Class.forName("org.hibernate.Version").getPackage().getImplementationVersion();
8+
} catch (Exception e) {
9+
// Should not happen: hibernate not found in the classpath
10+
throw new RuntimeException(e);
11+
}
12+
}
13+
14+
public static boolean isHibernate5_2_Plus(){
15+
String version = getHibernateVersion();
16+
String[] split = version.split("\\.");
17+
int isV5 = split[0].compareTo("5");
18+
if(isV5 != 0){
19+
return isV5 > 0;
20+
}
21+
int isV52 = split[1].compareTo("2");
22+
return isV52 >= 0;
23+
}
24+
25+
public static Class<?> getTransactionCoordinatorClass() {
26+
try {
27+
return Class.forName("org.hibernate.resource.transaction.TransactionCoordinator");
28+
} catch (ClassNotFoundException e) {
29+
try {
30+
return Class.forName("org.hibernate.resource.transaction.spi.TransactionCoordinator");
31+
} catch (Exception e2) {
32+
// should never happen
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
}
37+
38+
}

hibernate5/src/main/java/com/fasterxml/jackson/datatype/hibernate5/PersistentCollectionSerializer.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.datatype.hibernate5;
22

33
import java.io.IOException;
4+
import java.lang.reflect.Method;
45
import java.util.ArrayList;
56
import java.util.Collection;
67
import java.util.HashMap;
@@ -340,9 +341,7 @@ private void initializeCollection(PersistentCollection coll, Session session) {
340341
// .getTransactionFactory()
341342
// .compatibleWithJtaSynchronization();
342343
//Above is removed after Hibernate 5
343-
boolean isJTA = ((SessionImplementor) session).getTransactionCoordinator()
344-
.getTransactionCoordinatorBuilder()
345-
.isJta();
344+
boolean isJTA = SessionReader.isJTA(session);
346345

347346
if (!isJTA) {
348347
session.beginTransaction();
@@ -422,4 +421,55 @@ private Object convertToMap(Map<?, ?> value) {
422421
private Object convertToSet(Set<?> value) {
423422
return new HashSet<>(value);
424423
}
424+
425+
protected static class SessionReader {
426+
427+
/**
428+
* Return changed from org.hibernate.resource.transaction.TransactionCoordinator
429+
* to org.hibernate.resource.transaction.spi.TransactionCoordinator
430+
*/
431+
protected static final Method getTransactionCoordinatorMethod;
432+
/**
433+
* Return changed from org.hibernate.resource.transaction.TransactionCoordinatorBuilder
434+
* to org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder
435+
*/
436+
protected static final Method getTransactionCoordinatorBuilderMethod;
437+
/**
438+
* Class changed from org.hibernate.resource.transaction.TransactionCoordinatorBuilder
439+
* to org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder
440+
*/
441+
protected static final Method isJtaMethod;
442+
443+
static {
444+
try {
445+
getTransactionCoordinatorMethod = SessionImplementor.class.getMethod("getTransactionCoordinator");
446+
} catch (Exception e) {
447+
// should never happen: the class and method exists in all versions of hibernate 5
448+
throw new RuntimeException(e);
449+
}
450+
try{
451+
getTransactionCoordinatorBuilderMethod = Hibernate5Version.getTransactionCoordinatorClass().getMethod("getTransactionCoordinatorBuilder");
452+
} catch (Exception e) {
453+
// should never happen
454+
throw new RuntimeException(e);
455+
}
456+
try{
457+
isJtaMethod = Hibernate5Version.getTransactionCoordinatorClass().getMethod("isJta");
458+
} catch (Exception e) {
459+
// should never happen
460+
throw new RuntimeException(e);
461+
}
462+
}
463+
464+
public static boolean isJTA(Session session) {
465+
try {
466+
Object transactionCoordinator = getTransactionCoordinatorMethod.invoke(session);
467+
Object transactionCoordinatorBuilder = getTransactionCoordinatorBuilderMethod.invoke(transactionCoordinator);
468+
return (boolean) isJtaMethod.invoke(transactionCoordinatorBuilder);
469+
} catch (Exception e) {
470+
// Should never happen
471+
throw new RuntimeException(e);
472+
}
473+
}
474+
}
425475
}

hibernate5/src/test/java/com/fasterxml/jackson/datatype/hibernate5/BaseTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public abstract class BaseTest extends junit.framework.TestCase
1010
{
1111
protected BaseTest() {
1212
try {
13-
Logger.getLogger(this.getClass()).info("Testing using hibernate " + Class.forName("org.hibernate.Version").getMethod("getVersionString").invoke(null));
13+
System.out.println(Hibernate5Version.getHibernateVersion());
14+
System.out.println(Hibernate5Version.isHibernate5_2_Plus());
15+
Logger.getLogger(this.getClass()).info("Testing using hibernate " + Hibernate5Version.getHibernateVersion() +
16+
", is 5.2+: " + Hibernate5Version.isHibernate5_2_Plus());
1417
} catch (Exception e) {
1518
// Should not happen
1619
throw new RuntimeException(e);

0 commit comments

Comments
 (0)