Hibernate mapped data types! (change)

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.

 1
 2 Session session  =  sessionFactory.openSession();
 3 Transaction tx  =  session.beginTransaction();
 4 Customer customer  =   new  Customer();
 5 customer.setDescription(Hibernate.createClob( "" ));  // 先保存一个空的clob
 6 session.save(customer);
 7 session.flush();
 8 // 锁定这条记录
 9 session.refresh(customer,LockMode.UPGRADE);
10 oracle.sql.CLOB clob  =  (oracle.sql.CLOB) customer.getDescription();
11 java.io.Writer pw  =  clob.getCharacterOutStream();
12 pw.write(longText); // longText是一个长度超过255的字符串
13 pw.close();
14 tx.commit();
15 session.close();


* 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.

Guess you like

Origin blog.csdn.net/ever_who/article/details/80524366