Insert “YES” and “NO” to boolean filed type in DB (char(1), etc)


Hibernate has a built-in “yes_no” type that would do what you want. It maps to a CHAR(1) column in the database.

Basic mapping: <property name="some_flag" type="yes_no"/>

Annotation mapping (Hibernate extensions):

public boolean getFlag();

here is also a “true_false” type that will store either “T” or “F”.

A pure JPA solution:

As of 2013/2014 it is the best answer without using any Hibernate specific annotations, but please note this solution is JPA 2.1, and was not available when the question was first asked:

public class Person {    

    private Boolean isAlive;    

And then:

public class BooleanToStringConverter implements AttributeConverter<Boolean, String> {

    public String convertToDatabaseColumn(Boolean value) {        
        return (value != null && value) ? "Y" : "N";            

    public Boolean convertToEntityAttribute(String value) {
        return "Y".equals(value);