Siege Lion on the road (one) Hibernate (ix)---hibernate mapping type

Source: Internet
Author: User
Tags locale time and date

Hibernate takes a mapping type as a bridge between Java type and SQL type, corresponding to the type attribute.
There are two types: the built-in mapping type and the custom mapping type.
One, built-in mapping type :
1,JaVA basic types of Hibernate mapping types :

Types of hibernate mappings for Java underlying types
Hibernate mapping Type Java type Standard SQL type Size and range of values
Integer or int int or Java.lang.Integer INTEGER
Long Long BIGINT
Short Short SMALLINT
Byte Byte TINYINT
Float Float FLOAT
Double Double DOUBLE
Character Char\string CHAR (1) Fixed-length characters
String String VARCHAR Edge length string
Boolean Boolean BIT
Yes_no Boolean CHAR (1)
True_false Boolean CHAR (1)


2. Types of Hibernate mappings for Java time and date types :

Types of hibernate mappings for time-date types in Java
Mapping type Java type Standard SQL type Describe
Date Java.util.Date or Java.sql.Date DATE Date represented
Time Java. Util.date or Java.sql.Time Time Representative time
Timestamp Java. Util.date or Java.sql.Timpstamp TIMESTAMP Represent time and date
Calendar Java. Util.calendar TIMESTAMP Ditto
Calendar_date Java. Util.calendar DATE Date represented


3. Types of hibernate mappings for Java large object types :

Hibernate mapping types for Java large object types
Mapping type Java type Standard SQL type MySQL type Oracle Type
Binary Byte[] VARBINARY (or BLOB) Blob Blob
Text Java.lang.String Clob TEXT Clob
Serializable Classes that implement the Serializable interface VARBINARY (or BLOB) Blob Blob
Clob Java.sql.Clob Clob TEXT Clob
Blob Java.sql.Blob Blob Blob Blob


4.The Hibernate mapping type of the individual Java classes that the JDK comes with :

Hibernate mapping types for individual classes that are brought from the JDK
Mapping type Java type Standard SQL type
Class Java.lang.Class VARCHAR
Locale Java.util.Locale VARCHAR
TimeZone Java.util.TimeZone VARCHAR
Currency Java.util.Currency VARCHAR


Ii. Customized mapping type :
By implementing the Org.hibernate.usertype.UserType interface, it is possible to map a Java type to a SQL type.
1, Several methods of the interface description :

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 sqlTypes():设置该类型的字段对应的SQL类型。比如VARCHAR returnClass():设置该类型的字段对应的Java类型。 isMutabel():判断对应的Java类型是否为可变类。 deepCopy(Object value):该方法用于生成对应属性的快照。对于可变类,必须返回参数的复制值。 equals(Object x, Object y):比较对应属性的当前值和它的快照是否相同。 hashCode(Object x):不做解释。 nullSaveGet(ResultSet resultSet,String[] names, Ojbect owner):   当Hibernate从数据库加载对象时,调用该方法来取得该客户化类型的属性值。resultSet为JDBC的结果集,names为存放了表字段名的数组。在该方法内部实现从数据库字段到Java字段的转化。 nullSafeSet(PreparedStatement statement, Object value, int index):   当Hibernate将对象持久化到数据库时,调用该方法把对应的属性值添加到SQL insert语句中。在该方法内部完成SQL语句的参数指定。 assemble(Serializable cached, Object owner):   当Hibernate把二级缓存中的对象加载到Session缓存中时,调用该方法来获取对应属性的反序列化数据。如果参数cached为可变类型,则应该返回参数cached的快照(即调用deepCopy(cached)) disassemble(Object value):   当Hibernate把Session缓存中的对象保存到二级缓存中时,调用该方法获取对应属性的序列化数据。如果参数value为可变类型,则应该返回参数cached的快照(即调用deepCopy(value)) replace(Object original, Object target, Object owner):   当Session的merge()方法把一个游离对象A融合到持久化对象B时,会调用该方法来获得用于替代对象B对应属性的值。如果参数original为可变类型,则应该返回参数cached的快照(即调用deepCopy(original))

2. configuration file Use :

?
1 <property name="phone" type="xx.xx.MyType"column="PHONE"/>

3. Use this method instead of Hibernate components : The method is to encapsulate multiple SQL fields in an interface implementation as an address object.

4. Example code :

 Public classAddressusertypeImplementsusertype {Private Static Final int[] Sql_types ={Types.varchar,types.varchar,types.varchar,types.varchar};  Public int[] SqlTypes () {returnSql_types;}  PublicClass Returnedclass () {returnAddress.class; }  Public BooleanIsmutable () {return false; }  PublicObject Deepcopy (object value) {returnValue//Address is immutable  }   Public Booleanequals (object x, Object y) {if(x = = y)return true; if(x = =NULL|| y = =NULL)return false; returnx.equals (y); }   Public inthashcode (Object x) {returnX.hashcode (); }     PublicObject Nullsafeget (ResultSet resultset,string[] names, object owner)throwshibernateexception, SQLException {String province= Resultset.getstring (names[0]); String City= Resultset.getstring (names[1]); String Street= Resultset.getstring (names[2]); String ZipCode= Resultset.getstring (names[3]); if(Province = =NULL&& city==NULL&& street==NULL&& zipcode==NULL)      return NULL; return NewAddress (Province,city,street,zipcode); }   Public voidNullsafeset (PreparedStatement statement,object value,intindex)throwshibernateexception, SQLException {if(Value = =NULL) {statement.setnull (index, Types.varchar); Statement.setnull (Index+1, Types.varchar); Statement.setnull (Index+2, Types.varchar); Statement.setnull (Index+3, Types.varchar); } Else{Address Address=(Address) value;      Statement.setstring (Index, address.getprovince ()); Statement.setstring (Index+1, Address.getcity ()); Statement.setstring (Index+2, Address.getstreet ()); Statement.setstring (Index+3, Address.getzipcode ()); }  }   Publicobject Assemble (Serializable cached, object owner) {returncached; }   PublicSerializable disassemble (Object value) {return(Serializable) value; }     PublicObject Replace (object Original,object target,object owner) {returnoriginal; }}

manipulating BLOBs and CLOB type data :
In a persisted class, a binary large object can be declared as byte[] or java.sql.Blob; A string large object can be declared as a java.lang.String or a java.sql.Clob type.
No explanation at this stage.

( Disclaimer: This article is all from "proficient Hibernate:java object Persistence technology detailed" [Sun Weichen Electronic Industry Press] a book. The purpose of this article is only as a study note. If you need to reprint, please indicate the original book related information. )

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.