Skip to content

Commit 7adc00e

Browse files
author
Markus
committed
Fix issue
FasterXML#111
1 parent 9d3d46a commit 7adc00e

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

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

+50-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
package com.fasterxml.jackson.datatype.hibernate5;
22

3+
import javax.persistence.ElementCollection;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.ManyToMany;
7+
import javax.persistence.ManyToOne;
8+
import javax.persistence.OneToMany;
9+
import javax.persistence.OneToOne;
310
import java.io.IOException;
4-
import java.util.*;
5-
6-
import javax.persistence.*;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
import java.util.HashMap;
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Set;
718

819
import com.fasterxml.jackson.core.JsonGenerator;
9-
import com.fasterxml.jackson.databind.*;
20+
import com.fasterxml.jackson.databind.BeanProperty;
21+
import com.fasterxml.jackson.databind.JavaType;
22+
import com.fasterxml.jackson.databind.JsonMappingException;
23+
import com.fasterxml.jackson.databind.JsonSerializer;
24+
import com.fasterxml.jackson.databind.SerializerProvider;
1025
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1126
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1227
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
1328
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
1429
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
1530
import com.fasterxml.jackson.databind.util.NameTransformer;
1631
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
17-
1832
import org.hibernate.FlushMode;
1933
import org.hibernate.Hibernate;
2034
import org.hibernate.Session;
@@ -24,6 +38,8 @@
2438
import org.hibernate.engine.spi.SessionFactoryImplementor;
2539
import org.hibernate.engine.spi.SessionImplementor;
2640
import org.hibernate.mapping.Bag;
41+
import org.hibernate.resource.transaction.TransactionCoordinator;
42+
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl;
2743

2844
/**
2945
* Wrapper serializer used to handle aspects of lazy loading that can be used
@@ -89,7 +105,7 @@ protected PersistentCollectionSerializer(PersistentCollectionSerializer base, Js
89105
public PersistentCollectionSerializer unwrappingSerializer(NameTransformer unwrapper) {
90106
return _withSerializer(_serializer.unwrappingSerializer(unwrapper));
91107
}
92-
108+
93109
protected PersistentCollectionSerializer _withSerializer(JsonSerializer<?> ser) {
94110
if ((ser == _serializer) || (ser == null)) {
95111
return this;
@@ -261,7 +277,7 @@ public void serializeWithType(Object value, JsonGenerator g, SerializerProvider
261277

262278
// 30-Jul-2016, tatu: wrt [datatype-hibernate#93], conversion IS needed here (or,
263279
// if we could figure out, type id)
264-
280+
265281
// !!! TODO: figure out how to replace type id without having to replace collection
266282
if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) {
267283
value = convertToJavaCollection(value); // Strip PersistentCollection
@@ -281,7 +297,7 @@ protected ContainerSerializer<?> _containerSerializer() {
281297
}
282298
return null;
283299
}
284-
300+
285301
protected Object findLazyValue(PersistentCollection coll) {
286302
// If lazy-loaded, not yet loaded, may serialize as null?
287303
if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) {
@@ -400,17 +416,33 @@ private Object convertToMap(Map<?, ?> value) {
400416
private Object convertToSet(Set<?> value) {
401417
return new HashSet<>(value);
402418
}
403-
404-
protected static class SessionReader {
405-
public static boolean isJTA(Session session) {
406-
try {
407-
EntityManager em = (EntityManager) session;
408-
em.getTransaction();
409-
return false;
410-
} catch (IllegalStateException e) {
411-
// EntityManager is required to throw an IllegalStateException if it's JTA-managed
412-
return true;
419+
420+
protected static class SessionReader
421+
{
422+
public static boolean isJTA(Session session)
423+
{
424+
if (session instanceof EntityManager)
425+
{
426+
try
427+
{
428+
session.getTransaction();
429+
return false;
430+
}
431+
catch (final IllegalStateException e)
432+
{
433+
// EntityManager is required to throw an IllegalStateException if it's JTA-managed
434+
return true;
435+
}
436+
}
437+
else if (session instanceof SessionImplementor)
438+
{
439+
final TransactionCoordinator transactionCoordinator = ((SessionImplementor) session).getTransactionCoordinator();
440+
441+
return (transactionCoordinator instanceof JtaTransactionCoordinatorImpl);
413442
}
443+
444+
// If in doubt, do without (transaction)
445+
return true;
414446
}
415447
}
416448
}

0 commit comments

Comments
 (0)