ClassCastException when binding a MaterializedNClobType value as NClob
Description
org.hibernate.type.MaterializedNClobType has:
org.hibernate.type.descriptor.java.StringTypeDescriptor#INSTANCE as its JavaTypeDescriptor
org.hibernate.type.descriptor.sql.NClobTypeDescriptor#DEFAULT as its SqlTypeDescriptor
When org.hibernate.type.descriptor.sql.NClobTypeDescriptor binds a String as an NClob, it calls StringTypeDescriptor#unwrap to "unwrap" the NClob.
StringTypeDescriptor#unwrap checks if Clob is assignable from the expected type (NClob). Since NClob extends Clob, a Clob is created and returned. This ultimately causes a ClassCastException when binding the returned value as an NClob.
The fix simply moves the check for an NClob before the check for a Clob, so that an NClob will be returned instead.
Activity
Show:
Former user June 14, 2017 at 8:47 PM
Fixed in master and 5.1 branches.
I've included tests for with the test case added for this issue. (already fixed in master; will be fixed in 5.1 shortly).
org.hibernate.type.MaterializedNClobType
has:org.hibernate.type.descriptor.java.StringTypeDescriptor
#INSTANCE as itsJavaTypeDescriptor
org.hibernate.type.descriptor.sql.NClobTypeDescriptor#DEFAULT
as itsSqlTypeDescriptor
When
org.hibernate.type.descriptor.sql.NClobTypeDescriptor
binds aString
as anNClob
, it callsStringTypeDescriptor#unwrap
to "unwrap" theNClob
.StringTypeDescriptor#unwrap
checks ifClob
is assignable from the expected type (NClob
). SinceNClob
extendsClob
, aClob
is created and returned. This ultimately causes aClassCastException
when binding the returned value as anNClob
.The fix simply moves the check for an
NClob
before the check for aClob
, so that anNClob
will be returned instead.