@@ -46,6 +46,7 @@ public class HibernateProxySerializer
46
46
protected final boolean _forceLazyLoading ;
47
47
protected final boolean _serializeIdentifier ;
48
48
protected final boolean _nullMissingEntities ;
49
+ protected final boolean _wrappedIdentifier ;
49
50
protected final Mapping _mapping ;
50
51
51
52
/**
@@ -62,26 +63,31 @@ public class HibernateProxySerializer
62
63
63
64
public HibernateProxySerializer (boolean forceLazyLoading )
64
65
{
65
- this (forceLazyLoading , false , false , null , null );
66
+ this (forceLazyLoading , false , false , true , null , null );
66
67
}
67
68
68
69
public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier ) {
69
- this (forceLazyLoading , serializeIdentifier , false , null , null );
70
+ this (forceLazyLoading , serializeIdentifier , false , true , null , null );
70
71
}
71
72
72
73
public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier , Mapping mapping ) {
73
- this (forceLazyLoading , serializeIdentifier , false , mapping , null );
74
+ this (forceLazyLoading , serializeIdentifier , false , true , mapping , null );
74
75
}
75
76
76
77
public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier , boolean nullMissingEntities , Mapping mapping ) {
77
- this (forceLazyLoading , serializeIdentifier , nullMissingEntities , mapping , null );
78
+ this (forceLazyLoading , serializeIdentifier , nullMissingEntities , true , mapping , null );
78
79
}
79
80
80
- public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier , boolean nullMissingEntities , Mapping mapping ,
81
- BeanProperty property ) {
81
+ public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier , boolean nullMissingEntities , boolean wrappedIdentifier , Mapping mapping ) {
82
+ this (forceLazyLoading , serializeIdentifier , nullMissingEntities , wrappedIdentifier , mapping , null );
83
+ }
84
+
85
+ public HibernateProxySerializer (boolean forceLazyLoading , boolean serializeIdentifier , boolean nullMissingEntities , boolean wrappedIdentifier , Mapping mapping ,
86
+ BeanProperty property ) {
82
87
_forceLazyLoading = forceLazyLoading ;
83
88
_serializeIdentifier = serializeIdentifier ;
84
89
_nullMissingEntities = nullMissingEntities ;
90
+ _wrappedIdentifier = wrappedIdentifier ;
85
91
_mapping = mapping ;
86
92
_dynamicSerializers = PropertySerializerMap .emptyForProperties ();
87
93
_property = property ;
@@ -90,8 +96,8 @@ public HibernateProxySerializer(boolean forceLazyLoading, boolean serializeIdent
90
96
@ Override
91
97
public JsonSerializer <?> createContextual (SerializerProvider prov , BeanProperty property ) {
92
98
return new HibernateProxySerializer (this ._forceLazyLoading , _serializeIdentifier , _nullMissingEntities ,
93
- _mapping , property );
94
- }
99
+ _wrappedIdentifier , _mapping , property );
100
+ }
95
101
96
102
/*
97
103
/**********************************************************************
@@ -103,7 +109,7 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty
103
109
public boolean isEmpty (SerializerProvider provider , HibernateProxy value ) {
104
110
return (value == null ) || (findProxied (value ) == null );
105
111
}
106
-
112
+
107
113
@ Override
108
114
public void serialize (HibernateProxy value , JsonGenerator jgen , SerializerProvider provider )
109
115
throws IOException
@@ -181,29 +187,21 @@ protected JsonSerializer<Object> findSerializer(SerializerProvider provider, Obj
181
187
* Helper method for finding value being proxied, if it is available
182
188
* or if it is to be forced to be loaded.
183
189
*/
184
- protected Object findProxied (HibernateProxy proxy )
190
+ protected Object findProxied (final HibernateProxy proxy )
185
191
{
186
192
LazyInitializer init = proxy .getHibernateLazyInitializer ();
187
193
if (!_forceLazyLoading && init .isUninitialized ()) {
188
194
if (_serializeIdentifier ) {
189
- String idName ;
190
- if (_mapping != null ) {
191
- idName = _mapping .getIdentifierPropertyName (init .getEntityName ());
195
+ final Object idValue = init .getIdentifier ();
196
+ final Object result ;
197
+ if (_wrappedIdentifier ) {
198
+ final HashMap <String , Object > map = new HashMap <>();
199
+ map .put (getIdentifierPropertyName (init ), idValue );
200
+ result = map ;
192
201
} else {
193
- final SessionImplementor session = init .getSession ();
194
- if (session != null ) {
195
- idName = session .getFactory ().getIdentifierPropertyName (init .getEntityName ());
196
- } else {
197
- idName = ProxyReader .getIdentifierPropertyName (init );
198
- if (idName == null ) {
199
- idName = init .getEntityName ();
200
- }
201
- }
202
+ result = idValue ;
202
203
}
203
- final Object idValue = init .getIdentifier ();
204
- HashMap <String , Object > map = new HashMap <String , Object >();
205
- map .put (idName , idValue );
206
- return map ;
204
+ return result ;
207
205
}
208
206
return null ;
209
207
}
@@ -217,12 +215,36 @@ protected Object findProxied(HibernateProxy proxy)
217
215
}
218
216
}
219
217
}
220
-
218
+
219
+ /**
220
+ * Helper method to retrieve the name of the identifier property of the
221
+ * specified lazy initializer.
222
+ * @param init Lazy initializer to obtain identifier property name from.
223
+ * @return Name of the identity property of the specified lazy initializer.
224
+ */
225
+ private String getIdentifierPropertyName (final LazyInitializer init ) {
226
+ String idName ;
227
+ if (_mapping != null ) {
228
+ idName = _mapping .getIdentifierPropertyName (init .getEntityName ());
229
+ } else {
230
+ final SessionImplementor session = init .getSession ();
231
+ if (session != null ) {
232
+ idName = session .getFactory ().getIdentifierPropertyName (init .getEntityName ());
233
+ } else {
234
+ idName = ProxyReader .getIdentifierPropertyName (init );
235
+ if (idName == null ) {
236
+ idName = init .getEntityName ();
237
+ }
238
+ }
239
+ }
240
+ return idName ;
241
+ }
242
+
221
243
/**
222
244
* Inspects a Hibernate proxy to try and determine the name of the identifier property
223
- * (Hibernate proxies know the getter of the identifier property because it receives special
224
- * treatment in the invocation handler). Alas, the field storing the method reference is
225
- * private and has no getter, so we must resort to ugly reflection hacks to read its value ...
245
+ * (Hibernate proxies know the getter of the identifier property because it receives special
246
+ * treatment in the invocation handler). Alas, the field storing the method reference is
247
+ * private and has no getter, so we must resort to ugly reflection hacks to read its value ...
226
248
*/
227
249
protected static class ProxyReader {
228
250
@@ -235,7 +257,7 @@ protected static class ProxyReader {
235
257
getIdentifierMethodField .setAccessible (true );
236
258
} catch (Exception e ) {
237
259
// should never happen: the field exists in all versions of hibernate 4 and 5
238
- throw new RuntimeException (e );
260
+ throw new RuntimeException (e );
239
261
}
240
262
}
241
263
0 commit comments