Skip to content

Commit

Permalink
Fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus committed Aug 22, 2018
1 parent 9d3d46a commit 7adc00e
Showing 1 changed file with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package com.fasterxml.jackson.datatype.hibernate5;

import javax.persistence.ElementCollection;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import java.io.IOException;
import java.util.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
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 org.hibernate.FlushMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
Expand All @@ -24,6 +38,8 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.mapping.Bag;
import org.hibernate.resource.transaction.TransactionCoordinator;
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl;

/**
* Wrapper serializer used to handle aspects of lazy loading that can be used
Expand Down Expand Up @@ -89,7 +105,7 @@ protected PersistentCollectionSerializer(PersistentCollectionSerializer base, Js
public PersistentCollectionSerializer unwrappingSerializer(NameTransformer unwrapper) {
return _withSerializer(_serializer.unwrappingSerializer(unwrapper));
}

protected PersistentCollectionSerializer _withSerializer(JsonSerializer<?> ser) {
if ((ser == _serializer) || (ser == null)) {
return this;
Expand Down Expand Up @@ -261,7 +277,7 @@ public void serializeWithType(Object value, JsonGenerator g, SerializerProvider

// 30-Jul-2016, tatu: wrt [datatype-hibernate#93], conversion IS needed here (or,
// if we could figure out, type id)

// !!! TODO: figure out how to replace type id without having to replace collection
if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) {
value = convertToJavaCollection(value); // Strip PersistentCollection
Expand All @@ -281,7 +297,7 @@ protected ContainerSerializer<?> _containerSerializer() {
}
return null;
}

protected Object findLazyValue(PersistentCollection coll) {
// If lazy-loaded, not yet loaded, may serialize as null?
if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) {
Expand Down Expand Up @@ -400,17 +416,33 @@ private Object convertToMap(Map<?, ?> value) {
private Object convertToSet(Set<?> value) {
return new HashSet<>(value);
}

protected static class SessionReader {
public static boolean isJTA(Session session) {
try {
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;

protected static class SessionReader
{
public static boolean isJTA(Session session)
{
if (session instanceof EntityManager)
{
try
{
session.getTransaction();
return false;
}
catch (final IllegalStateException e)
{
// EntityManager is required to throw an IllegalStateException if it's JTA-managed
return true;
}
}
else if (session instanceof SessionImplementor)
{
final TransactionCoordinator transactionCoordinator = ((SessionImplementor) session).getTransactionCoordinator();

return (transactionCoordinator instanceof JtaTransactionCoordinatorImpl);
}

// If in doubt, do without (transaction)
return true;
}
}
}

0 comments on commit 7adc00e

Please sign in to comment.