Type of mapping for hibernate

Source: Internet
Author: User
Tags time and date

Tag: Type auto false cannot ast specify bit record shorthand

Hibernate mapping type hibernate mysql mapping type

1. Hibernate mapping type hibernate mysql mapping type

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

Mapping type

Java type

Standard SQL Type

MySQL type

Oracle type

Binary

Byte[]

VARBINARY (or BLOB)

Blob

Blob

Text

String

Clob

TEXT

Clob

Serializable

Serializable interface arbitrary implementation class

VARBINARY (or BLOB)

Blob

Blob

Clob

Java.sql.Clob

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:

Save an empty Blob or Clob instance in a database transaction first.

Then lock the record, update the saved blob or Clob instance, and write the binary or text data to the BLOB or Clob instance

Describes the MySQL data type, after all, for the data you want to choose a suitable type, for the database access efficiency has been greatly improved.
There are several types of MySQL databases: integer (int,binint,smallint,tinyint, etc. xxxint), floating-point (float,double,real, etc.), fixed-point type (decimal, The legendary floating-point string representation), date and Time (Data,time,datetime,timestamp), String type (Char,varchar,text,tinytext,mediumtext,longtext, etc.) , Binary (Tinyblob,tinyblob,blob and other BLOB type), enumerated (Enum,set--set is similar to enum type).

The following is a detailed description of the basic information of these types, starting with the integer type:
integer Type
By default, the int type includes both positive and negative numbers, and if the unsigned attribute is defined for the Int column, then its value range is always positive. This is always positive, very important, that is, if you are working on a data column: Update tablename set Int_field = (int_field-1000), if the value of Int_field is less than 1000, it is theoretically turned into a negative number, But since you set the attribute to be unsigned, the stored data is still a unsigned positive number, which can give you false or confusing results.
The value range of the tinyint is from -128~+127, and if the unsigned attribute is used, then its value range becomes 0~+255. If a user tries to deposit a number that exceeds the range of field values, MySQL does a very simple job, which is replaced directly with the maximum or minimum desirable value.
It may be noted that when we use phpMyAdmin, if we choose the int type, then there is an optional length in the back. However, do not be confused by this value, because its actual function does not specify the length of the int type, but the maximum display width (m:maximum display width), can only be used to query the data when the query results according to the width you specify to display, The main use is for typesetting needs. Although this is the case, in some rare cases (MySQL performs some complex queries that need to be done with a temporary data table), the values of the temporary data table may be truncated and result in an incorrect end result. Therefore, it is recommended that you leave it blank unless you are performing all operations with the database at the command line.

int type

The actual meaning of the type

 tinyint (m)

8-bit integer, occupies 1 bytes ( -128~+127)

 smallint (m)

16-bit integer, 2 bytes ( -32768~+32767)

 medimumint (m)

24-bit integer, 3 bytes ( -8388608~+8388607 )

 int (m), Integer (m)

32-bit integer, 4 bytes (-214 7483648~+2147483647)

 bigint (m)

64-bit integer, Takes 8 bytes ( -9.22E+18 ~ +9.22E+18)

 serial

This is actually a shorthand for bigint auto_increment NOT null primary key

1, this property must be used in conjunction with not NULL, primary Key, or unique attribute.
2, each data table can have only one data column of the Auto_increment property
3, this property is generally when the data is inserted, the value is not explicitly specified, or the specified value is null, only function. If a value is specified and the value has not yet occurred, MySQL will use that value to insert to generate a new record. At this time there are two situations: a) The original data is 1~100, and later deleted the 20~80 content, that is, the database only 1~19,81~100 data, when inserting the data ID 20, the database will follow the rules, 20 this record inserted into, and will not error, The self-increment is still 100, and the next time normal insert, the value of 101 will be used by default. b) The original data is 1~100, I insert the data ID 1000, will not error, but since the increment to 1000, the next time the insertion data from 1001. (verbose, but should remember)
4, if you want to know what the data value you just inserted, after inserting the data, you can use the Select LAST_INSERT_ID () statement to get. In PHP, there is a function is: mysql_insert_id (), the function is not very recommended, because it returns the data is type int, if the auto_increment data column type is bigint, and the actual value has exceeded the maximum value of int type, The value returned by mysql_insert_id () will not be correct.
5, if the auto_increment counter reaches the maximum value (that is, the maximum allowable value for the field), it will no longer increment, so data insertion cannot be performed. In fact, after the maximum value, MySQL will always insert the maximum value of the field into the database, resulting in the MySQL report: The data column already exists error.
6, if it is feasible, or if the expected number is very large, try to use the type of data you want to set the next level, but try not to exaggerate. For example, you expect your users to be no more than 10 billion, the surface of the medimum unsigned already enough, but at this time, it is still recommended that you use the int type, just in case.

There is also a bit and bool, in MySQL, the keyword bool is a synonym for tinyint, in 5.0.2 and in previous versions, bit is the same. But starting with 5.0.3, bit is no longer synonymous with tinyint, but a new data type that can store up to 64 bits of binary value. This will be introduced separately in the future, here is a stroke of the first.

Type of mapping for hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.