I’m reading data from a table( from a MySQL Database) with Hibernate SQL Query. The thing is, the table contains a colum that is mapped to a char in Hibernate Model, and sometimes this column is empty. And I suppose this is where my exception comes from. How can I map a colum of char to my hibernate model without getting this error ? Thanks for your answers !
Thank you for your answer ! My column is not nullable (I ‘m using MySQL and this column is NOT NULL) Then, I don’t think that
if (str == null) {
is appropriate.
the error is :
15:30:35,289 INFO CharacterType:178 - could not read column value from result set: LSFUS11_20_; String index out of range: 0
which results in the following exception :
java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:558)
I think I may try your solution, but with :
if (str == '') {
since it can’t be null, it’s just an empty String.
Thanks for your piece code, I’m going to try that !
I am assuming from your question that you’re mapping this to a primitive character. Next time, please post the stacktrace that you receive (you may leave out where you call it, you could only include the hibernate stuff if your project is too sensitive).
If you do map to a primitive character, and it is null, you will get an exception, because primitives cannot have null assigned to them.
This class will mitigate this, the ‘null’ character is returned as a character representing ‘0’. You can customize this to your liking:
To use this new type, in your hibernate mapping, before you had something like:
Now, you just specify the class name as your type:
However, the best practice is to not use primitive types for database mapping. If at all possible, use Character instead of char, because that way you won’t have an issue with null (null can be assigned to the wrapper types).