Hibernate and CHAR(%n) mapping

Programming Add comments

By default, Hibernate seems to map CHAR(%n) database fields to java.lang.String. It causes no problem when querying a database without comparing this field, but unfortunately, the query may fail if the String does not containt %n chars. To avoid any problem, I suggest using VARCHAR(%n) fields where %n is the max size that can have your string. If you prefer to keep CHAR(%n) fields, probably for performance matters, refer to the hibernate documentation to specify a type such as “type=CharArray’” in your mapping.

The consequence of automatically mapping a database chararray field to a String object in java causes the impossibility of using this field in WHERE clauses.

Leave a Reply