1
1
package com .fasterxml .jackson .datatype .hibernate5 ;
2
2
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 ;
3
10
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 ;
7
18
8
19
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 ;
10
25
import com .fasterxml .jackson .databind .jsonFormatVisitors .JsonFormatVisitorWrapper ;
11
26
import com .fasterxml .jackson .databind .jsontype .TypeSerializer ;
12
27
import com .fasterxml .jackson .databind .ser .ContainerSerializer ;
13
28
import com .fasterxml .jackson .databind .ser .ContextualSerializer ;
14
29
import com .fasterxml .jackson .databind .ser .ResolvableSerializer ;
15
30
import com .fasterxml .jackson .databind .util .NameTransformer ;
16
31
import com .fasterxml .jackson .datatype .hibernate5 .Hibernate5Module .Feature ;
17
-
18
32
import org .hibernate .FlushMode ;
19
33
import org .hibernate .Hibernate ;
20
34
import org .hibernate .Session ;
24
38
import org .hibernate .engine .spi .SessionFactoryImplementor ;
25
39
import org .hibernate .engine .spi .SessionImplementor ;
26
40
import org .hibernate .mapping .Bag ;
41
+ import org .hibernate .resource .transaction .TransactionCoordinator ;
42
+ import org .hibernate .resource .transaction .backend .jta .internal .JtaTransactionCoordinatorImpl ;
27
43
28
44
/**
29
45
* Wrapper serializer used to handle aspects of lazy loading that can be used
@@ -89,7 +105,7 @@ protected PersistentCollectionSerializer(PersistentCollectionSerializer base, Js
89
105
public PersistentCollectionSerializer unwrappingSerializer (NameTransformer unwrapper ) {
90
106
return _withSerializer (_serializer .unwrappingSerializer (unwrapper ));
91
107
}
92
-
108
+
93
109
protected PersistentCollectionSerializer _withSerializer (JsonSerializer <?> ser ) {
94
110
if ((ser == _serializer ) || (ser == null )) {
95
111
return this ;
@@ -261,7 +277,7 @@ public void serializeWithType(Object value, JsonGenerator g, SerializerProvider
261
277
262
278
// 30-Jul-2016, tatu: wrt [datatype-hibernate#93], conversion IS needed here (or,
263
279
// if we could figure out, type id)
264
-
280
+
265
281
// !!! TODO: figure out how to replace type id without having to replace collection
266
282
if (Feature .REPLACE_PERSISTENT_COLLECTIONS .enabledIn (_features )) {
267
283
value = convertToJavaCollection (value ); // Strip PersistentCollection
@@ -281,7 +297,7 @@ protected ContainerSerializer<?> _containerSerializer() {
281
297
}
282
298
return null ;
283
299
}
284
-
300
+
285
301
protected Object findLazyValue (PersistentCollection coll ) {
286
302
// If lazy-loaded, not yet loaded, may serialize as null?
287
303
if (!Feature .FORCE_LAZY_LOADING .enabledIn (_features ) && !coll .wasInitialized ()) {
@@ -400,17 +416,33 @@ private Object convertToMap(Map<?, ?> value) {
400
416
private Object convertToSet (Set <?> value ) {
401
417
return new HashSet <>(value );
402
418
}
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 );
413
442
}
443
+
444
+ // If in doubt, do without (transaction)
445
+ return true ;
414
446
}
415
447
}
416
448
}
0 commit comments