Changing the default type mapping

Sometimes we are interested in changing JPA's default type mapping strategy. For example Mysql™ versions prior to 5.0 lack an appropriate type representing boolean values. It was therefore quite common mapping boolean properties to CHAR(1) with possible values being 'Y' and 'N'. Hibernate will map boolean values to tinyint(1). Supporting older software may require to tweak the standard mapping.

Unfortunately JPA itself does not offer any interface for this purpose. The persistence provider may offer a solution though. Hibernate for example allows to remap ❶ types . We assume our hibintro.v9.User class to have a boolean property active:

package hibintro.v9;

import org.hibernate.type.Type;
public class User {
   public void setCname(String cname) {this.cname = cname;}

   boolean active = false;
   @Type(type="yes_no") public boolean isActive() {return active;}
   public void setActive(boolean active) { = active;}
   uidNumber int(11) NOT NULL PRIMARY KEY,
   active char(1) NOT NULL,
   cname varchar(255) DEFAULT NULL,
   uid varchar(255) NOT NULL

Readers being interested in more sophisticated strategies like mapping user defined data types to database types are advised to read the manual section on Hibernate types.