tags:

views:

2838

answers:

4

Hello ! 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 !

+1  A: 

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:

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.type.CharacterType;

public class NullCharacterType extends CharacterType {

    /**
     * Serializable ID generated by Eclipse
     */
    private static final long serialVersionUID = 1L;

    public NullCharacterType() {
        super();
    }

    public Object get(final ResultSet rs, final String name)
      throws SQLException {
     final String str = rs.getString(name);
     if (str == null || str.length() == 0) {
      return new Character((char) 0);
     } else {
      return new Character(str.charAt(0));
     }
    }
}

To use this new type, in your hibernate mapping, before you had something like:

<property name="theChar" type="character">

Now, you just specify the class name as your type:

<property name="theChar" type="yourpackage.NullCharacterType">

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).

MetroidFan2002
A: 

Hi ! 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 !

Anthony43
My code can be easily edited, I'll provide a newer example.
MetroidFan2002
See the edits to my code above. You shouldn't use "" for an empty string check, just in case code naively calls new String("");, length() == 0 is a better check (it doesn't have the overhead of "".equals(String);
MetroidFan2002
@Anthony - PLEASE don't write an ANSWER if you are NOT answering the original question. Use comments to comment! or edit your question to complement it.
Carlos Heuberger
A: 

Anthony43, please don't use == to compare objects. It does not work. You compare object identities that way, and not object values.

if (str == "")

may work if the object is not dynamic, but will fail if the String is constructed.

The following code:

System.out.println( "" == "" );

String s = "";
System.out.println(s == "");

StringBuilder b = new StringBuilder();
b.append("");
System.out.println("b = '" + b.toString() + "'");
System.out.println( b.toString() == "");

demonstrates the problem. The output is:

true
true
b = ''
false

The dynamic String (e.g. not known at compile time) will use a different object. And hibernate constructs objects dynamically.

As for testing for an empty string, I personally prefer something like

str.length() == 0

or when I'm not interested in strings with only whitespace

str.trim().length() == 0

I would very much appreciate if you correct your code as it may confuse future readers of your response.

extraneon
A: 

search ur jboss(server) whether mysql.jar(mysql-connector-java-5.1.7-bin) is present in lib files or not. even i faced the same problem, after adding the mysql.jar file it is working fine.

srinivas