There are two types of hibernate mappings: The built-in mapping type and the custom mapping type. The built-in mapping type is responsible for mapping some common Java types to the appropriate SQL type, and hibernate also allows users to implement Usertype or Compositeusertype interfaces to flexibly customize custom mapping types. A custom type can map a user-defined Java type to a corresponding field in a database table.
First, Hibernate's built-in mapping type
1. Types of hibernate mappings for Java basic types
Hibernate Mapping type |
Java Type |
Standard SQL type |
Size and range of values |
integer or int |
int or java.lang.Integer |
INTEGER |
4 bytes |
Long |
Long Long |
BIGINT |
8 bytes |
Short |
Short Short |
SMALLINT |
2 bytes |
Byte |
BYTE byte |
TINYINT |
1 bytes |
Float |
float float |
FLOAT |
4 bytes |
Double |
Double Double |
DOUBLE |
8 bytes |
Big_decimal |
Java.math.BigDecimal |
NUMERIC |
NUMERIC (8,2) 8 -bit |
Character |
Char Character String |
CHAR (1) |
Fixed-length characters |
String |
String |
VARCHAR |
Variable length string |
Boolean |
Boolean Boolean |
BIT |
Boolean type |
Yes_no |
Boolean Boolean |
CHAR (1) (y-n) |
Boolean type |
True_false |
Boolean Boolean |
CHAR (1) (T-F) |
Boolean type |
2. Hibernate mappings for Java time and date types
Mapping type |
Java Type |
Standard SQL type |
Describe |
Date |
util. Date or sql. Date |
DATE |
Yyyy-mm-dd |
Time |
Date time |
Time |
HH:MM:SS |
Timestamp |
Date Timestamp |
TIMESTAMP |
Yyyymmddhhmmss |
Calendar |
Calendar |
TIMESTAMP |
Yyyymmddhhmmss |
Calendar_date |
Calendar |
DATE |
Yyyy-mm-dd |
3. Types of hibernate mappings for Java large object types
style= "font-family: ' Times New Roman ';" >serializable interface arbitrary implementation class
Map type |
style= font-family Times New Roman '; " >java type |
Standard sql type |
mysql type |
oracle type |
binary |
style= "font-family: ' Times New Roman ';" >byte[] |
varbinary ( or blob) |
blob |
blob |
text |
style= "font-family: ' Times New Roman ';" >string |
clob |
text |
clob |
serializable | TD valign= "Top" width= "
varbinary ( or blob) |
blob |
blob |
clob |
style= "font-family: ' Times New Roman ';" >java.sql.clob |
style= "font-family: ' Times New Roman ';" >clob |
text |
clob |
Blob |
Java.sql.Blob |
Blob |
Blob |
Blob |
When you save a Java.sql.Clob or Java.sql.Blob instance through hibernate in your program, you must include two steps:
L Save an empty blob or CLOB instance in a database transaction first.
L then lock the record, update the saved blob or CLOB instance, and write the binary data or text data to the BLOB or CLOB instance.
4. Hibernate mapping type of individual Java classes with JDK (slightly)
5. Use hibernate built-in mapping type
Ii. Customized mapping type (slightly)
End!
Hibernate Tutorial-mapping types