Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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).</p> <p>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. </p> <p>This class will mitigate this, the "null" character is returned as a character representing "0". You can customize this to your liking:</p> <pre><code>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)); } } } </code></pre> <p>To use this new type, in your hibernate mapping, before you had something like:</p> <pre><code>&lt;property name="theChar" type="character"&gt; </code></pre> <p>Now, you just specify the class name as your type:</p> <pre><code>&lt;property name="theChar" type="yourpackage.NullCharacterType"&gt; </code></pre> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload