In this article by Ahmad Seddighi, we will discuss the Hibernate types. We will see how Hibernate provides built-in types that map to common database types. We'll also see how Hibernate allows us to implement and use custom types when these built-in types do not satisfy the application's requirements, or when we want to change the default behavior of a built-in type. As you will see, you can easily implement a custom-type class and then use it in the same way as a built-in one.
Hibernate allows transparent persistence, which means the application is absolutely isolated from the underlying database storage format. Three players in the Hibernate scene implement this feature: Hibernate dialect, Hibernate types, and HQL. The Hibernate dialect allows us to use a range of different databases, supporting different, proprietary variants of SQL and column types. In addition, HQL allows us to query persisted objects, regardless of their relational persisted form in the database.
Hibernate types are a representation of databases SQL types, provide an abstraction of the underlying database types, and prevent the application from getting involved with the actual database column types. They allow us to develop the application without worrying about the target database and the column types that the database supports. Instead, we get involved with mapping Java types to Hibernate types. The database dialect, as part of Hibernate, is responsible for transforming Java types to SQL types, based on the target database. This gives us the flexibility to change the database to one that may support different column types or SQL without changing the application code.
Built-in types
Hibernate includes a rich and powerful range of built-in types. These types satisfy most needs of a typical application, providing a bridge between basic Java types and common SQL types. Java types mapped with these types range from basic, simple types, such as long and int, to large and complex types, such as Blob and Clob. The following table categorizes Hibernate built-in types with corresponding Java and SQL types:
|
Java Type |
Hibernate Type Name |
SQL Type |
|
Primitives |
||
|
Boolean or boolean |
boolean |
BIT |
|
true_false |
CHAR(1)('T'or'F') |
|
|
yes_no |
CHAR(1)('Y'or'N') |
|
|
Byte or byte |
byte |
TINYINT |
|
char or Character |
character |
CHAR |
|
double or Double |
double |
DOUBLE |
|
float or float |
float |
FLOAT |
|
int or Integer |
integer |
INTEGER |
|
long or Long |
long |
BIGINT |
|
short or Short |
short |
SMALLINT |
|
String |
||
|
java.lang.String |
string |
VARCHAR |
|
character |
CHAR(1) |
|
|
text |
CLOB |
|
|
Arbitrary Precision Numeric |
||
|
java.math.BigDecimal |
big_decimal |
NUMERIC |
|
Byte Array |
||
|
byte[] or Byte[] |
binary |
VARBINARY |
|
Time and Date |
||
|
java.util.Date |
date |
DATE |
|
time |
TIME |
|
|
timestamp |
TIMESTAMP |
|
|
java.util.Calendar |
calendar |
TIMESTAMP |
|
calendar_date |
DATE |
|
|
java.sql.Date |
date |
DATE |
|
java.sql.Time |
time |
TIME |
|
java.sql.Timestamp |
timestamp |
TIMESTAMP |
|
Localization |
||
|
java.util.Locale |
locale |
VARCHAR |
|
java.util.TimeZone |
timezone |
|
|
java.util.Currency |
currency |
|
|
Class Names |
||
|
java.lang.Class |
class |
VARCHAR |
|
Any Serializable Object |
||
|
java.io.Serializable |
Serializable |
VARBINARY |
|
JDBC Large Objects |
||
|
java.sql.Blob |
blob |
BLOB |
|
java.sql.Clob |
clob |
CLOB |
Sign up for a Packt account to see the rest of this article
Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.
Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.




Post new comment