Skip to content

Commit dff74af

Browse files
committed
hibernate4
1 parent d0d98f0 commit dff74af

File tree

9 files changed

+29
-40
lines changed

9 files changed

+29
-40
lines changed

hibernate4/src/main/java/tools/jackson/datatype/hibernate4/HibernateProxySerializer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tools.jackson.datatype.hibernate4;
22

33
import java.beans.Introspector;
4-
import java.io.IOException;
54
import java.lang.reflect.Field;
65
import java.lang.reflect.Method;
76
import java.util.HashMap;
@@ -17,6 +16,7 @@
1716
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1817
import tools.jackson.databind.jsontype.TypeSerializer;
1918
import tools.jackson.databind.ser.impl.PropertySerializerMap;
19+
import tools.jackson.databind.type.TypeFactory;
2020
import tools.jackson.databind.util.NameTransformer;
2121

2222
import org.hibernate.engine.spi.Mapping;
@@ -170,7 +170,6 @@ public boolean isEmpty(SerializationContext provider, HibernateProxy value) {
170170

171171
@Override
172172
public void serialize(HibernateProxy value, JsonGenerator jgen, SerializationContext provider)
173-
throws IOException
174173
{
175174
Object proxiedValue = findProxied(value);
176175
// TODO: figure out how to suppress nulls, if necessary? (too late for that here)
@@ -184,7 +183,6 @@ public void serialize(HibernateProxy value, JsonGenerator jgen, SerializationCon
184183
@Override
185184
public void serializeWithType(HibernateProxy value, JsonGenerator jgen, SerializationContext provider,
186185
TypeSerializer typeSer)
187-
throws IOException
188186
{
189187
Object proxiedValue = findProxied(value);
190188
if (proxiedValue == null) {
@@ -220,7 +218,6 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
220218
*/
221219

222220
protected ValueSerializer<Object> findSerializer(SerializationContext provider, Object value)
223-
throws IOException
224221
{
225222
/* TODO: if Hibernate did use generics, or we wanted to allow use of Jackson
226223
* annotations to indicate type, should take that into account.
@@ -234,7 +231,11 @@ protected ValueSerializer<Object> findSerializer(SerializationContext provider,
234231
* really anyone's guess at this point; proxies can exist at any level?
235232
*/
236233
PropertySerializerMap.SerializerAndMapResult result =
237-
_dynamicSerializers.findAndAddPrimarySerializer(type, provider, _property);
234+
_dynamicSerializers.findAndAddPrimarySerializer(
235+
//TODO find better way to get JavaType
236+
TypeFactory.createDefaultInstance().unsafeSimpleType(type),
237+
provider,
238+
_property);
238239
if (_dynamicSerializers != result.map) {
239240
_dynamicSerializers = result.map;
240241
}

hibernate4/src/main/java/tools/jackson/datatype/hibernate4/HibernateSerializers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tools.jackson.datatype.hibernate4;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import tools.jackson.databind.BeanDescription;
45
import tools.jackson.databind.JavaType;
56
import tools.jackson.databind.ValueSerializer;
@@ -32,7 +33,7 @@ public HibernateSerializers(Mapping mapping, int features)
3233

3334
@Override
3435
public ValueSerializer<?> findSerializer(SerializationConfig config,
35-
JavaType type, BeanDescription.Supplier beanDesc)
36+
JavaType type, BeanDescription.Supplier beanDesc, JsonFormat.Value formatOverrides)
3637
{
3738
Class<?> raw = type.getRawClass();
3839
if (HibernateProxy.class.isAssignableFrom(raw)) {

hibernate4/src/main/java/tools/jackson/datatype/hibernate4/PersistentCollectionSerializer.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tools.jackson.databind.*;
1010
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1111
import tools.jackson.databind.jsontype.TypeSerializer;
12+
import tools.jackson.databind.ser.std.StdContainerSerializer;
1213
import tools.jackson.databind.util.NameTransformer;
1314
import tools.jackson.datatype.hibernate4.Hibernate4Module.Feature;
1415

@@ -28,7 +29,7 @@
2829
* and <code>Map</code> types (unlike in JDK).
2930
*/
3031
public class PersistentCollectionSerializer
31-
extends ContainerSerializer<Object>
32+
extends StdContainerSerializer<Object>
3233
{
3334
private static final long serialVersionUID = 1L; // since 2.7
3435

@@ -61,7 +62,7 @@ public class PersistentCollectionSerializer
6162
@SuppressWarnings("unchecked")
6263
public PersistentCollectionSerializer(JavaType containerType,
6364
ValueSerializer<?> serializer, int features, SessionFactory sessionFactory) {
64-
super(containerType);
65+
super(containerType, null);
6566
_originalType = containerType;
6667
_serializer = (ValueSerializer<Object>) serializer;
6768
_features = features;
@@ -95,9 +96,9 @@ protected PersistentCollectionSerializer _withSerializer(ValueSerializer<?> ser)
9596

9697
// from `ContainerSerializer`
9798
@Override
98-
protected ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts)
99+
protected StdContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts)
99100
{
100-
ContainerSerializer<?> ser0 = _containerSerializer();
101+
StdContainerSerializer<?> ser0 = _containerSerializer();
101102
if (ser0 != null) {
102103
return _withSerializer(ser0.withValueTypeSerializer(vts));
103104
}
@@ -145,19 +146,6 @@ public ValueSerializer<?> createContextual(SerializationContext provider,
145146
/**********************************************************************
146147
*/
147148

148-
@Deprecated // since 2.5
149-
@Override
150-
public boolean isEmpty(Object value) {
151-
if (value == null) { // is null ever passed?
152-
return true;
153-
}
154-
if (value instanceof PersistentCollection) {
155-
Object lazy = findLazyValue((PersistentCollection) value);
156-
return (lazy == null) || _serializer.isEmpty(lazy);
157-
}
158-
return _serializer.isEmpty(value);
159-
}
160-
161149
@Override
162150
public boolean isEmpty(SerializationContext provider, Object value)
163151
{
@@ -196,7 +184,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
196184

197185
@Override
198186
public JavaType getContentType() {
199-
ContainerSerializer<?> ser = _containerSerializer();
187+
StdContainerSerializer<?> ser = _containerSerializer();
200188
if (ser != null) {
201189
return ser.getContentType();
202190
}
@@ -205,7 +193,7 @@ public JavaType getContentType() {
205193

206194
@Override
207195
public ValueSerializer<?> getContentSerializer() {
208-
ContainerSerializer<?> ser = _containerSerializer();
196+
StdContainerSerializer<?> ser = _containerSerializer();
209197
if (ser != null) {
210198
return ser.getContentSerializer();
211199
}
@@ -232,7 +220,6 @@ public boolean hasSingleElement(Object value) {
232220

233221
@Override
234222
public void serialize(Object value, JsonGenerator jgen, SerializationContext provider)
235-
throws IOException
236223
{
237224
if (value instanceof PersistentCollection) {
238225
value = findLazyValue((PersistentCollection) value);
@@ -255,7 +242,6 @@ public void serialize(Object value, JsonGenerator jgen, SerializationContext pro
255242
@Override
256243
public void serializeWithType(Object value, JsonGenerator jgen, SerializationContext provider,
257244
TypeSerializer typeSer)
258-
throws IOException
259245
{
260246
if (value instanceof PersistentCollection) {
261247
value = findLazyValue((PersistentCollection) value);
@@ -281,9 +267,9 @@ public void serializeWithType(Object value, JsonGenerator jgen, SerializationCon
281267
/**********************************************************************
282268
*/
283269

284-
protected ContainerSerializer<?> _containerSerializer() {
285-
if (_serializer instanceof ContainerSerializer) {
286-
return (ContainerSerializer<?>) _serializer;
270+
protected StdContainerSerializer<?> _containerSerializer() {
271+
if (_serializer instanceof StdContainerSerializer stdContainerSerializer) {
272+
return stdContainerSerializer;
287273
}
288274
return null;
289275
}

hibernate4/src/test/java/tools/jackson/datatype/hibernate4/InclusionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.fasterxml.jackson.annotation.JsonInclude.Include;
88
import tools.jackson.databind.ObjectMapper;
9+
import tools.jackson.databind.json.JsonMapper;
910

1011
import static org.junit.jupiter.api.Assertions.assertEquals;
1112

hibernate4/src/test/java/tools/jackson/datatype/hibernate4/LazyLoadingTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import javax.persistence.EntityManagerFactory;
77
import javax.persistence.Persistence;
88

9-
import tools.jackson.core.JsonProcessingException;
9+
import tools.jackson.core.JacksonException;
1010
import tools.jackson.databind.ObjectMapper;
11+
import tools.jackson.databind.json.JsonMapper;
1112
import tools.jackson.datatype.hibernate4.Hibernate4Module.Feature;
1213
import tools.jackson.datatype.hibernate4.data.Customer;
1314
import tools.jackson.datatype.hibernate4.data.Payment;
@@ -60,10 +61,10 @@ public void testGetCustomerJson() throws Exception
6061
}
6162

6263
@Test
63-
public void testSerializeIdentifierFeature() throws JsonProcessingException {
64+
public void testSerializeIdentifierFeature() throws JacksonException {
6465
Hibernate4Module module = new Hibernate4Module();
6566
module.enable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
66-
ObjectMapper objectMapper = new ObjectMapper().registerModule(module);
67+
ObjectMapper objectMapper = JsonMapper.builder().addModule(module).build();
6768

6869
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
6970
try {

hibernate4/src/test/java/tools/jackson/datatype/hibernate4/OneToManyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.jupiter.api.Test;
88

99
import tools.jackson.databind.ObjectMapper;
10+
import tools.jackson.databind.json.JsonMapper;
1011

1112
import static org.junit.jupiter.api.Assertions.assertEquals;
1213

@@ -42,7 +43,7 @@ public void testMapWithOneToMany() throws Exception {
4243
}
4344

4445
private String mapWithHibernateModule(Object object) throws Exception {
45-
return new ObjectMapper().registerModule(new Hibernate4Module()).writeValueAsString(object);
46+
return JsonMapper.builder().addModule(new Hibernate4Module()).build().writeValueAsString(object);
4647
}
4748

4849
private String mapWithoutHibernateModule(Object object) throws Exception {

hibernate4/src/test/java/tools/jackson/datatype/hibernate4/TransientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonView;
99

1010
import tools.jackson.databind.ObjectMapper;
11+
import tools.jackson.databind.json.JsonMapper;
1112

1213
import static org.junit.jupiter.api.Assertions.assertEquals;
1314

@@ -55,7 +56,7 @@ public void testSimpleTransient() throws Exception
5556
// and then with Transient disabled
5657
Hibernate4Module mod = hibernateModule(false);
5758
mod.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION);
58-
mapper = new ObjectMapper().registerModule(mod);
59+
mapper = JsonMapper.builder().addModule(mod).build();
5960

6061
assertEquals(aposToQuotes("{'a':1,'b':2}"), mapper.writeValueAsString(new WithTransient()));
6162
}

hibernate4/src/test/java/tools/jackson/datatype/hibernate4/UnwrappedTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonUnwrapped;
5-
import tools.jackson.core.JsonProcessingException;
5+
import tools.jackson.core.JacksonException;
66
import tools.jackson.core.type.TypeReference;
77
import tools.jackson.databind.ObjectMapper;
88
import tools.jackson.datatype.hibernate4.data.Customer;
@@ -13,7 +13,6 @@
1313
import javax.persistence.EntityManager;
1414
import javax.persistence.EntityManagerFactory;
1515
import javax.persistence.Persistence;
16-
import java.util.Map;
1716

1817
import static org.junit.jupiter.api.Assertions.*;
1918

@@ -40,7 +39,7 @@ public T getContent()
4039
}
4140

4241
@Test
43-
public void testSimpleUnwrapped() throws JsonProcessingException
42+
public void testSimpleUnwrapped() throws JacksonException
4443
{
4544
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
4645
try {

pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
</description>
1515

1616
<modules>
17-
<!--
1817
<module>hibernate4</module>
19-
-->
2018
<module>hibernate5</module>
2119
<!-- added in 2.13.0 to support JAXB->Jakarta move: -->
2220
<module>hibernate5-jakarta</module>

0 commit comments

Comments
 (0)