Using Boolean fields with Oracle RDBMS

Like I posted here we are currently busy replacing the Postgres database with an Oracle database. One issue we encountered was the use of boolean fields in the Postgres database, because there is no such type in the Oracle database. So we chose to create a Varchar2(1) field that contains a ‘t’ or ‘f’, being ‘true’ or ‘false’.
But since our application has already been written for the biggest part, we are already making use of boolean fields in our Java code. But that’s where Hibernate comes to the rescue. We are using Hibernate3 as our ORM framework. With Hibernate it is very easy to convert a boolean in the Java code to a char value in the database by extending the class ‘CharBooleanType’ to create your own custom type. I have the java code we used for our type here:

package net.pascalalma.hibernate.types;

import org.hibernate.type.CharBooleanType;

public class TrueFalseType extends CharBooleanType {

	protected final String getTrueString() {
		return "t";

	protected final String getFalseString() {
		return "f";

To use this type in our Entity classes I have defined the type with a package annotation (yes, we are using Hibernate annotations at our project). This package scoped annotation is a nice feature since JDK5 which I hadn’t used or seen before. I have created a file ‘’ like this:

  {@TypeDef(name="truefalse-type",typeClass = TrueFalseType.class)}
package net.pascalalma.hibernate.entity;

import net.pascalalma.hibernate.types.TrueFalseType;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

(By the way, I read somewhere that the javadoc generator will take the javadoc in this as package javadoc, even if there is also a package.html defined in this package)
Now in the entity classes in the package ‘net.pascalalma.hibernate.entity’ I can use the defined Type like this:

package net.pascalalma.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;


public class MyTable {

	Long id;
	Boolean indicator;
	String name;

	public String getName() {
		return name;
	public void setName(String name) { = name;
	public Long getId() {
		return id;
	public void setId(Long id) { = id;

	public Boolean getIndicator() {
		return indicator;

	public void setIndicator(Boolean indicator) {
		this.indicator = indicator;

And that’s it. No more changes aren necessary to solve this issue, so we can continue our conversion untill we run into the next issue 🙂

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Hibernate and tagged . Bookmark the permalink.

4 Responses to Using Boolean fields with Oracle RDBMS

  1. mehtak says:

    I tried the above in my code and got “Could not determine type for: truefalse-type” . Do I need to place the in any specific directory. How does the SessionFactory know about this file so that it can determine the class to use.

  2. mehtak says:

    I guess I figured out what I was doing wrong. I have to place in the same package as the classes using the type-defs. I should have read the article properly. Nice blog.

  3. Pascal Alma says:

    Okay, I was just checking my code. Yes, you are right, you have to put the in the package where you want to use/refer to the type.

  4. Ranjay says:

    Very nice and to the point article.

Comments are closed.