Hibernate mapping types are divided into two types: built-in mapping types and customized mapping types. The built-in mapping type is responsible for mapping some common Java types to the corresponding SQL types; in addition, Hibernate also allows users to implement the UserType or CompositeUserType interface to flexibly customize customized mapping types
1. Built-in mapping Type
1).Hibernate mapping type of Java basic type
Java types | Hibernate mapping type | Standard SQL types | Size and value range |
int/Integer | int/integer | INTEGER | 4Byte |
long/Long | long | BIGINT | 8Byte |
short/Short | short | SAMLLINT | 2Byte |
exchange/Exchange | byte | TINYINT | 1Byte |
float/Float | float | FLOAT | 4Byte |
double/Double | double | DOUBLE | 8Byte |
BigDecimal | big_decimal | NUMBERIC | Numeric(8,2) |
char/Character/String | character | CHAR(1) | Fixed length characters |
String | string | VARCHAR | variable length characters |
boolean/Boolean | boolean | BIT | Boolean type |
boolean/Boolean | yes/no | CHAR(1)('Y'/'N') | Boolean type |
boolean/Boolean | true/false | CHAR(1)('T'/'F') | Boolean type |
2). Hibernate mapping type of Java time and date types
Java types | Hibernate mapping type | Standard SQL types | describe |
java.util.Date/java.sql.Date | date | DATE | Date, yyyy-mm-dd |
java.util.Date/java.sql.TIme | time | TIME | Time,hh:mm:ss |
java.util.Date/java.sql.Timestamp | timestamp | TIMESTAMP | Timestamp, yyyymmddhhmmss |
java.util.Calendar | calendar | TIMESTAMP | Same as above |
java.util.Calendar | calendar_date | DATE | Date, yyyy-mm-dd |
* When the program type is java.sql.Timestamp and the table attribute type in the database is timestamp, even if the user inserts data with a null value, the database system will still automatically fill in the timestamp value
3). Hibernate mapping type of Java large object type
Java types | Hibernate mapping type | Standard SQL types | MySql type | Oracle type |
byte[] | binary | VARBINARY/BLOB | BLOB | BLOB |
String | text | CLOB | TEXT | CLOB |
serializable | A java class that implements the serializable interface | VARBINARY/BLOB | BLOB | BLOB |
java.sql.Clob | clob | CLOB | TEXT | CLOB |
java.sql.Blob | blob | BLOB | BLOB | BLOB |
* When saving java.sql.Clob or java.sql.Blob instances through Hibernate in an application, two steps must be included:
a. In a database transaction First save an empty Blob or Clob instance; b. Then lock this record, update the Blob or Clob instance saved in step (1), and write binary data or long text data to the Blob or Clob instance.
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/dot.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
* When one java type corresponds to multiple Hibernate mapping types. For example, if the attribute of the persistence class is of type java.util.Date, the corresponding Hibernate mapping type can be date, time
or timestamp. At this time, the Hibernate mapping type must be determined based on the SQL type of the corresponding database table field. If the field is of type Date, hibernate maps it to datge, if it is TIME it is time, if it is TIMESTAMP it is timestamp.