@@ -41,8 +41,8 @@ public final class HibernateEntityValueEncoder<E> implements ValueEncoder<E>
41
41
42
42
private final Logger logger ;
43
43
44
- public HibernateEntityValueEncoder (Class <E > entityClass , PersistentClass persistentClass , Session session ,
45
- PropertyAccess propertyAccess , TypeCoercer typeCoercer , Logger logger )
44
+ public HibernateEntityValueEncoder (Class <E > entityClass , PersistentClass persistentClass ,
45
+ Session session , PropertyAccess propertyAccess , TypeCoercer typeCoercer , Logger logger )
46
46
{
47
47
this .entityClass = entityClass ;
48
48
this .session = session ;
@@ -53,39 +53,58 @@ public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persist
53
53
54
54
idPropertyName = property .getName ();
55
55
56
- propertyAdapter = propertyAccess .getAdapter (this .entityClass ).getPropertyAdapter (idPropertyName );
56
+ propertyAdapter = propertyAccess .getAdapter (this .entityClass ).getPropertyAdapter (
57
+ idPropertyName );
57
58
}
58
59
59
-
60
60
public String toClient (E value )
61
61
{
62
- if (value == null ) return null ;
62
+ if (value == null )
63
+ return null ;
63
64
64
65
Object id = propertyAdapter .get (value );
65
66
66
67
if (id == null )
67
- throw new IllegalStateException (String .format (
68
- "Entity %s has an %s property of null; this probably means that it has not been persisted yet." ,
69
- value , idPropertyName ));
68
+ throw new IllegalStateException (
69
+ String
70
+ .format (
71
+ "Entity %s has an %s property of null; this probably means that it has not been persisted yet." ,
72
+ value , idPropertyName ));
70
73
71
74
return typeCoercer .coerce (id , String .class );
72
75
}
73
76
74
77
@ SuppressWarnings ("unchecked" )
75
78
public E toValue (String clientValue )
76
79
{
77
- if (InternalUtils .isBlank (clientValue )) return null ;
80
+ if (InternalUtils .isBlank (clientValue ))
81
+ return null ;
82
+
83
+ Object id = null ;
84
+
85
+ try
86
+ {
78
87
79
- Object id = typeCoercer .coerce (clientValue , propertyAdapter .getType ());
88
+ id = typeCoercer .coerce (clientValue , propertyAdapter .getType ());
89
+ }
90
+ catch (Exception ex )
91
+ {
92
+ throw new RuntimeException (String .format (
93
+ "Exception converting '%s' to instance of %s (id type for entity %s): %s" ,
94
+ clientValue , propertyAdapter .getType ().getName (), entityClass .getName (),
95
+ InternalUtils .toMessage (ex )), ex );
96
+ }
80
97
81
98
Serializable ser = Defense .cast (id , Serializable .class , "id" );
82
99
83
100
E result = (E ) session .get (entityClass , ser );
84
101
85
102
if (result == null )
86
103
{
87
- // We don't identify the entity type in the message because the logger is based on the entity type.
88
- logger .error (String .format ("Unable to convert client value '%s' into an entity instance." , clientValue ));
104
+ // We don't identify the entity type in the message because the logger is based on the
105
+ // entity type.
106
+ logger .error (String .format (
107
+ "Unable to convert client value '%s' into an entity instance." , clientValue ));
89
108
}
90
109
91
110
return result ;
0 commit comments