I have a persistend Book Class with the following properties
There are two ways you can achieve the type conversion of attribute
By annotating field with Type
@Type(type = "yes_no")
private boolean isActive;
in DB Y/N will get persisted.
By writing a converter
@Column
@Convert(converter = BooleanConverter.class)
private boolean isActive;
Converter class
public class BooleanConverter implements AttributeConverter<Boolean, Character> {
@Override
public Character convertToDatabaseColumn(Boolean attribute) {
if (attribute)
return 'Y';
else
return 'N';
}
@Override
public Boolean convertToEntityAttribute(Character dbData) {
if ('Y' == dbData)
return true;
else
return false;
}
}
in xml you can replace the type
attribute with the converter class name or yes_no
<property name="status" column="book_status" type="yes_no" not-null="true"/>
From what I see you are trying to map the BIT type in Database to Boolean in your hibernate code.
There is a bug in MySQL with BIT Value, from version 5.0.3 onwards, in that it does not store a single BIT value. It stores something like SET or ENUM. And that often throws up issues, when you are doing a numeric value comparison. For more details check here
http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/
You could ask your DBA to change the datatype to tinyint, however if that is not possible, you can change the status mapping from boolean to numeric_boolean, so would be something like
<property name="status" column="book_status" type="numeric_boolean" not-null="true"/>