Skip to content

Commit 256eba4

Browse files
committed
Cleaning up fix for #93, adding some notes on javadoc
1 parent c900ad4 commit 256eba4

File tree

5 files changed

+78
-86
lines changed

5 files changed

+78
-86
lines changed

hibernate3/src/main/java/com/fasterxml/jackson/datatype/hibernate3/Hibernate3Module.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,41 @@ public enum Feature {
2929
USE_TRANSIENT_ANNOTATION(true),
3030

3131
/**
32-
* This feature determines how {@link org.hibernate.collection.PersistentCollection}s properties
33-
* for which no annotation is found are handled with respect to
34-
* lazy-loading: if true, lazy-loading is only assumed if annotation
35-
* is used to indicate that; if false, lazy-loading is assumed to be
36-
* the default.
37-
* Note that {@link #FORCE_LAZY_LOADING} has priority over this Feature;
38-
* meaning that if it is defined as true, setting of this Feature has no
39-
* effect.
32+
* This feature determines how {@link org.hibernate.collection.PersistentCollection}s properties
33+
* for which no annotation is found are handled with respect to
34+
* lazy-loading: if true, lazy-loading is only assumed if annotation
35+
* is used to indicate that; if false, lazy-loading is assumed to be
36+
* the default.
37+
* Note that {@link #FORCE_LAZY_LOADING} has priority over this Feature;
38+
* meaning that if it is defined as true, setting of this Feature has no
39+
* effect.
4040
* <p>
41-
* Default value is false, meaning that laziness is considered to be the
42-
* default value.
43-
*
44-
* @since 2.4
45-
*/
46-
REQUIRE_EXPLICIT_LAZY_LOADING_MARKER(false),
41+
* Default value is false, meaning that laziness is assumed by default,
42+
* without requiring marker.
43+
*
44+
* @since 2.4
45+
*/
46+
REQUIRE_EXPLICIT_LAZY_LOADING_MARKER(false),
4747

4848
/**
49-
* Replaces org.hibernate.collection.spi.PersistentCollection List, Set, Map subclasses to java.util.ArrayList, HashSet,
50-
* HashMap, during Serialization.
49+
* Feature that may be enabled to force
50+
* replacement <code>org.hibernate.collection.spi.PersistentCollection</code>,
51+
* <code>List</code>, <code>Set</code>, <code>Map</code> subclasses
52+
* during serialization as standard JDK {@lin java.util.List},
53+
* {@link java.util.Set} and {@link java.util.Map}.
54+
* This is usually done to prevent issues with polymorphic handling, so
55+
* that type id is generated for standard containers and NOT for Hibernate
56+
* variants.
5157
* <p>
52-
* Default is false.
58+
* Default setting is false, so that no replacement occurs.
5359
*
5460
* @since 2.8.2
5561
*/
5662
REPLACE_PERSISTENT_COLLECTIONS(false)
5763
;
5864

5965
final boolean _defaultState;
60-
final int _mask;
66+
final int _mask;
6167

6268
/**
6369
* Method that calculates bit set (flags) of all features that

hibernate4/src/main/java/com/fasterxml/jackson/datatype/hibernate4/Hibernate4Module.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ public enum Feature {
5656
REQUIRE_EXPLICIT_LAZY_LOADING_MARKER(false),
5757

5858
/**
59-
* Replaces org.hibernate.collection.spi.PersistentCollection List, Set, Map subclasses to java.util.ArrayList, HashSet,
60-
* HashMap, during Serialization.
59+
* Feature that may be enabled to force
60+
* replacement <code>org.hibernate.collection.spi.PersistentCollection</code>,
61+
* <code>List</code>, <code>Set</code>, <code>Map</code> subclasses
62+
* during serialization as standard JDK {@lin java.util.List},
63+
* {@link java.util.Set} and {@link java.util.Map}.
64+
* This is usually done to prevent issues with polymorphic handling, so
65+
* that type id is generated for standard containers and NOT for Hibernate
66+
* variants.
6167
* <p>
62-
* Default is false.
68+
* Default setting is false, so that no replacement occurs.
6369
*
6470
* @since 2.8.2
6571
*/

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
public class Hibernate5Module extends Module
1010
{
11-
1211
/**
1312
* Enumeration that defines all toggleable features this module
1413
*/
@@ -56,10 +55,16 @@ public enum Feature {
5655
REQUIRE_EXPLICIT_LAZY_LOADING_MARKER(false),
5756

5857
/**
59-
* Replaces org.hibernate.collection.spi.PersistentCollection List, Set, Map subclasses to java.util.ArrayList, HashSet,
60-
* HashMap, during Serialization.
58+
* Feature that may be enabled to force
59+
* replacement <code>org.hibernate.collection.spi.PersistentCollection</code>,
60+
* <code>List</code>, <code>Set</code>, <code>Map</code> subclasses
61+
* during serialization as standard JDK {@lin java.util.List},
62+
* {@link java.util.Set} and {@link java.util.Map}.
63+
* This is usually done to prevent issues with polymorphic handling, so
64+
* that type id is generated for standard containers and NOT for Hibernate
65+
* variants.
6166
* <p>
62-
* Default is false.
67+
* Default setting is false, so that no replacement occurs.
6368
*
6469
* @since 2.8.2
6570
*/

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ public void serialize(Object value, JsonGenerator jgen, SerializerProvider provi
259259
throw JsonMappingException.from(jgen, "PersistentCollection does not have serializer set");
260260
}
261261

262-
if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) {
263-
value = convertToJavaCollection(value); // Strip PersistentCollection
264-
}
265-
262+
// 30-Jul-2016, tatu: wrt [datatype-hibernate#93], should NOT have to do anything here;
263+
// only affects polymophic cases
266264
_serializer.serialize(value, jgen, provider);
267265
}
268266

@@ -282,10 +280,13 @@ public void serializeWithType(Object value, JsonGenerator jgen, SerializerProvid
282280
throw JsonMappingException.from(jgen, "PersistentCollection does not have serializer set");
283281
}
284282

283+
// 30-Jul-2016, tatu: wrt [datatype-hibernate#93], conversion IS needed here (or,
284+
// if we could figure out, type id)
285+
286+
// !!! TODO: figure out how to replace type id without having to replace collection
285287
if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) {
286288
value = convertToJavaCollection(value); // Strip PersistentCollection
287289
}
288-
289290
_serializer.serializeWithType(value, jgen, provider, typeSer);
290291
}
291292

@@ -389,6 +390,7 @@ protected boolean usesLazyLoading(BeanProperty property) {
389390
return false;
390391
}
391392

393+
// since 2.8.2
392394
private Object convertToJavaCollection(Object value) {
393395
if (!(value instanceof PersistentCollection)) {
394396
return value;
@@ -398,17 +400,15 @@ private Object convertToJavaCollection(Object value) {
398400
return convertToSet((Set<?>) value);
399401
}
400402

401-
if (value instanceof List
402-
|| value instanceof Bag
403-
) {
403+
if (value instanceof List || value instanceof Bag) {
404404
return convertToList((List<?>) value);
405405
}
406406

407407
if (value instanceof Map) {
408408
return convertToMap((Map<?, ?>) value);
409409
}
410410

411-
throw new IllegalArgumentException("Unsupported type: " + value.getClass());
411+
throw new IllegalArgumentException("Unsupported PersistentCollection subtype: " + value.getClass());
412412
}
413413

414414
private Object convertToList(List<?> value) {

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

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.fasterxml.jackson.datatype.hibernate5;
22

3+
import java.util.Set;
4+
35
import com.fasterxml.jackson.databind.JsonMappingException;
46
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
58
import com.fasterxml.jackson.datatype.hibernate5.data.Customer;
69
import com.fasterxml.jackson.datatype.hibernate5.data.Payment;
710
import org.hibernate.Hibernate;
@@ -13,22 +16,22 @@
1316
import javax.persistence.EntityManager;
1417
import javax.persistence.EntityManagerFactory;
1518
import javax.persistence.Persistence;
16-
import java.util.Map;
17-
import java.util.Set;
18-
19-
public class ReplacePersistentCollectionTest {
2019

20+
public class ReplacePersistentCollectionTest extends BaseTest
21+
{
2122
private EntityManagerFactory emf;
2223

23-
private EntityManager em;
24+
private EntityManager em;
2425

26+
@Override
2527
@Before
2628
public void setUp() throws Exception {
2729
emf = Persistence.createEntityManagerFactory("persistenceUnit");
28-
em = emf.createEntityManager();
29-
}
30+
em = emf.createEntityManager();
31+
}
3032

3133
@After
34+
@Override
3235
public void tearDown() throws Exception {
3336
em.close();
3437
emf.close();
@@ -40,31 +43,22 @@ public void testNoReplacePersistentCollection() throws Exception {
4043
final ObjectMapper mapper = new ObjectMapper()
4144
.registerModule(new Hibernate5Module()
4245
.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, true)
43-
).enableDefaultTyping();
46+
).enableDefaultTyping(DefaultTyping.NON_FINAL);
4447

4548
Customer customer = em.find(Customer.class, 103);
4649
Assert.assertFalse(Hibernate.isInitialized(customer.getPayments()));
4750
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
4851
Assert.assertTrue(json.contains("org.hibernate.collection"));
4952
// should force loading...
5053
Set<Payment> payments = customer.getPayments();
51-
/*
52-
System.out.println("--- JSON ---");
53-
System.out.println(json);
54-
System.out.println("--- /JSON ---");
55-
*/
56-
5754
Assert.assertTrue(Hibernate.isInitialized(payments));
58-
// TODO: verify
59-
Assert.assertNotNull(json);
60-
61-
boolean exceptionThrown = false;
55+
6256
try {
63-
Map<?, ?> stuff = mapper.readValue(json, Map.class);
57+
/*Customer result =*/ mapper.readValue(json, Customer.class);
58+
fail("Should throw exception");
6459
} catch (JsonMappingException e) {
65-
exceptionThrown = true;
60+
verifyException(e, "failed to lazily initialize");
6661
}
67-
Assert.assertTrue(exceptionThrown);
6862
}
6963

7064
// [Issue#93], backwards compatible case
@@ -74,37 +68,18 @@ public void testReplacePersistentCollection() throws Exception {
7468
.registerModule(new Hibernate5Module()
7569
.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, true)
7670
.configure(Hibernate5Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)
77-
).enableDefaultTyping();
78-
79-
Customer customer = em.find(Customer.class, 103);
80-
Assert.assertFalse(Hibernate.isInitialized(customer.getPayments()));
81-
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
82-
Assert.assertFalse(json.contains("org.hibernate.collection"));
83-
// should force loading...
84-
Set<Payment> payments = customer.getPayments();
85-
/*
86-
System.out.println("--- JSON ---");
87-
System.out.println(json);
88-
System.out.println("--- /JSON ---");
89-
*/
90-
91-
Assert.assertTrue(Hibernate.isInitialized(payments));
92-
// TODO: verify
93-
Assert.assertNotNull(json);
94-
95-
/*
96-
* Currently this cannot be verified due to Issue#94 default typing fails on 2.7.0 - 2.8.2-SNAPSHOT,
97-
* commented out until that is fixed.
98-
*/
99-
100-
boolean issue94failed = false;
101-
try {
102-
Map<?, ?> stuff = mapper.readValue(json, Map.class);
103-
} catch (JsonMappingException e) {
104-
issue94failed = true;
105-
}
106-
107-
Assert.assertTrue("If this fails, means #94 is fixed. Replace to the below commented lines", issue94failed);
71+
).enableDefaultTyping(DefaultTyping.NON_FINAL);
72+
73+
Customer customer = em.find(Customer.class, 103);
74+
Assert.assertFalse(Hibernate.isInitialized(customer.getPayments()));
75+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
76+
Assert.assertFalse(json.contains("org.hibernate.collection"));
77+
// should force loading...
78+
Set<Payment> payments = customer.getPayments();
79+
80+
Assert.assertTrue(Hibernate.isInitialized(payments));
81+
Customer stuff = mapper.readValue(json, Customer.class);
82+
assertNotNull(stuff);
10883

10984
// Map<?, ?> stuff = mapper.readValue(json, Map.class);
11085
//

0 commit comments

Comments
 (0)