How to get value as String object instead of Character object

In Hibernate 6.3.1.Final, if column is nvarchar(1), then value come as Character object while using session native query. Is there any way to get values as String object instead of Character object ?

You could call toString() on the result or you can construct the native query with the proper result type e.g. session.createNativeQuery( "...", String.class )

We are actually migrating from 5.4.32.Final to 6.3.1.Final. In our application, there are more than 1000 session native queries. In the previous version, the value of nvarchar(1) came as a string object. So we used cast to convert the result to a string. But in the latest version, values are coming as character objects. So it throws a ClassCastException as we are expecting those values to be strings. So is there any way to get a value as a string object? If we use session.createNativeQuery(“…”, String.class), it will only return string results. But there can also be other data types.

So is there any way to get a value as a string object?

There is, but I would recommend you to not do that and instead fix your queries as changing this behavior might be surprising in other scenarios. You can override the Dialect#resolveSqlTypeDescriptor to return a custom VarcharJdbcType/NVarcharJdbcType. These custom JdbcType implementations then override the getJdbcRecommendedJavaTypeMapping method and always return typeConfiguration.getJavaTypeRegistry().getDescriptor(String.class).

If we use session.createNativeQuery(“…”, String.class), it will only return string results. But there can also be other data types.

I don’t know what you mean by “but there can also be other data types”. If you mean to say that you might have multiple select items for which you want to read the values, then you should look into @SqlResultSetMapping.

2 Likes