| 6 March 2008 |
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:
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 'package-info.java' like this:
-
@TypeDefs(
-
{@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 package-info.java 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;
-
-
@Table(name="MYTABLE")
-
-
public class MyTable {
-
-
Long id;
-
Boolean indicator;
-
String name;
-
-
return name;
-
}
-
this.name = name;
-
}
-
@Id
-
@Column(name="ID")
-
return id;
-
}
-
this.id = id;
-
}
-
-
@Column(name="indicator")
-
@Type(type="truefalse-type")
-
return indicator;
-
}
-
-
this.indicator = indicator;
-
}
-
}
And that's it. More changes aren't necessary to solve this issue, so we can continue our conversion untill we run into the next issue :-)


4 comments to 'Using Boolean fields with Oracle RDBMS'
10 April 2008
I tried the above in my code and got "Could not determine type for: truefalse-type" . Do I need to place the package-info.java in any specific directory. How does the SessionFactory know about this file so that it can determine the class to use.
10 April 2008
I guess I figured out what I was doing wrong. I have to place package-info.java in the same package as the classes using the type-defs. I should have read the article properly. Nice blog.
10 April 2008
Okay, I was just checking my code. Yes, you are right, you have to put the package-info.java in the package where you want to use/refer to the type.
30 October 2008
Very nice and to the point article.