Hibernate Object Mapping Type

Source: Internet
Author: User
Tags locale

Hibernate understands both the Java and JDBC representations of application data. The ability to read and write object data to a database are called marshalling, and is the function of a Hibernate type. A is A type implementation of the org.hibernate.type.Type interface. A Hibernate type describes various aspects of behavior of the Java type such as how to check for equality and what to Clon E values.

Usage of the word type

A Hibernate is type neither a Java type nor a SQL datatype. It provides information about both of these.

When you encounter the term type in regards to Hibernate, it could refer to the Java type, the JDBC type, or the Hi Bernate type, depending on context.

1. Value types1.1. Basic types

Basic value types usually map a single database value, or column, to a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended in the JDBC Specificati Ons. You can override these mappings and provide and use alternative mappings. These topics is discussed further on.

Table 1.1. Basic Type Mappings

Hibernate Type Database Type JDBC Type Type Registry
Org.hibernate.type.StringType String VARCHAR String, java.lang.String
Org.hibernate.type.MaterializedClob String Clob Materialized_clob
Org.hibernate.type.TextType String LongVarChar Text
Org.hibernate.type.CharacterType Char, Java.lang.Character CHAR Char, Java.lang.Character
Org.hibernate.type.BooleanType Boolean BIT Boolean, Java.lang.Boolean
Org.hibernate.type.NumericBooleanType Boolean INTEGER, 0 is False, 1 is true Numeric_boolean
Org.hibernate.type.YesNoType Boolean CHAR, ' n '/' n ' is false, ' y '/' y ' is true. The uppercase value is written to the database. Yes_no
Org.hibernate.type.TrueFalseType Boolean CHAR, ' F '/' F ' is false, ' t '/' is true. The uppercase value is written to the database. True_false
Org.hibernate.type.ByteType BYTE, Java.lang.Byte TINYINT BYTE, Java.lang.Byte
Org.hibernate.type.ShortType Short, Java.lang.Short SMALLINT Short, Java.lang.Short
Org.hibernate.type.IntegerTypes int, Java.lang.Integer INTEGER int, Java.lang.Integer
Org.hibernate.type.LongType Long, Java.lang.Long BIGINT Long, Java.lang.Long
Org.hibernate.type.FloatType float, java.lang.Float FLOAT float, java.lang.Float
Org.hibernate.type.DoubleType Double, java.lang.Double DOUBLE Double, java.lang.Double
Org.hibernate.type.BigIntegerType Java.math.BigInteger NUMERIC Big_integer
Org.hibernate.type.BigDecimalType Java.math.BigDecimal NUMERIC Big_decimal, Java.math.bigDecimal
Org.hibernate.type.TimestampType Java.sql.Timestamp TIMESTAMP Timestamp, Java.sql.Timestamp
Org.hibernate.type.TimeType Java.sql.Time Time Time, Java.sql.Time
Org.hibernate.type.DateType Java.sql.Date DATE Date, Java.sql.Date
Org.hibernate.type.CalendarType Java.util.Calendar TIMESTAMP Calendar, Java.util.Calendar
Org.hibernate.type.CalendarDateType Java.util.Calendar DATE Calendar_date
Org.hibernate.type.CurrencyType Java.util.Currency VARCHAR Currency, java.util.Currency
Org.hibernate.type.LocaleType Java.util.Locale VARCHAR Locale, Java.utility.locale
Org.hibernate.type.TimeZoneType Java.util.TimeZone VARCHAR, using the TimeZone ID TimeZone, Java.util.TimeZone
Org.hibernate.type.UrlType Java.net.URL VARCHAR URL, Java.net.URL
Org.hibernate.type.ClassType Java.lang.Class VARCHAR, using the class name Class, Java.lang.Class
Org.hibernate.type.BlobType Java.sql.Blob Blob Blog, Java.sql.Blob
Org.hibernate.type.ClobType Java.sql.Clob Clob CLOB, Java.sql.Clob
Org.hibernate.type.BinaryType Primitive byte[] VARBINARY Binary, byte[]
Org.hibernate.type.MaterializedBlobType Primitive byte[] Blob Materized_blob
Org.hibernate.type.ImageType Primitive byte[] LongVarBinary Image
Org.hibernate.type.BinaryType Java.lang.byte[] VARBINARY Wrapper-binary
Org.hibernate.type.CharArrayType Char[] VARCHAR Characters, char[]
Org.hibernate.type.CharacterArrayType Java.lang.character[] VARCHAR Wrapper-characters, character[], java.lang.character[]
Org.hibernate.type.UUIDBinaryType Java.util.UUID BINARY Uuid-binary, Java.util.UUID
Org.hibernate.type.UUIDCharType Java.util.UUID CHAR, can also read VARCHAR Uuid-char
Org.hibernate.type.PostgresUUIDType Java.util.UUID PostgreSQL UUID, through Types#other, which complies to the PostgreSQL JDBC driver definition Pg-uuid
Org.hibernate.type.SerializableType Implementors of Java.lang.Serializable VARBINARY Unlike the other value types, multiple instances of this type is registered. It is registered once under Java.io.Serializable, and registered under the specific Java.io.Serializable implementation CL The names.

1.2. National Character Types

National Character types, which are a new feature since JDBC 4.0 API, now available in Hibernate type System. National Language support enables you retrieve data or insert data to a database in any character set that the Underlyin G Database supports.

Depending on your environment, you might want to set the configuration option Hibernate.use_nationalized_character_dat A to true and have all string or CLOB based attributes have this national character support automatically. There is nothing else to being changed, and you don ' t has to use any hibernate specific mapping, so it's portable (though The national character support feature are not required, and may not be work on other JPA provider Impl).

The other-to-be-the-feature is has the annotation on the @Nationalized attribute that should be nationalized. This is only works on string based attributes, including string, char, char array and CLOB.

                @Entity (name= "nationalizedentity") public                static class Nationalizedentity {                    @Id                    private Integer Id;                    @Nationalized                    private String Nvarcharatt;                    @Lob                    @Nationalized                    private String Materializednclobatt;                    @Lob                    @Nationalized                    private NClob Nclobatt;                    @Nationalized                    private Character Ncharacteratt;                    @Nationalized                    private character[] Nchararratt;                    @Type (Type = "ntext")                    private String Nlongvarcharcharatt;                }

Table 1.2. National Character Type Mappings

Hibernate type Database type JDBC type type registry
Org.hibernate.type.StringNVarcharType String NVARCHAR Nstring
Org.hibernate.type.NTextType String Longnvarchar Materialized_clob
Org.hibernate.type.NClobType Java.sql.NClob NCLOB Nclob
Org.hibernate.type.MaterializedNClobType String NCLOB Materialized_nclob
Org.hibernate.type.PrimitiveCharacterArrayNClobType Char[] NCHAR Char[]
Org.hibernate.type.CharacterNCharType Java.lang.Character NCHAR Ncharacter
Org.hibernate.type.CharacterArrayNClobType Java.lang.character[] NCLOB Character[], java.lang.character[]

1.3. Composite types

Composite types, or embedded types, as they is called by the Java persistence API, with traditionally b Een called in Hibernate. All of these terms mean the same thing.

Components represent aggregations of values to a single Java type. An example Address is a class, which aggregates street, city, state, and postal code. A composite type behaves in a similar-to-an entity. They is each classes written specifically for an application. They may both include references to other application-specific classes, as-well-to-collections and simple JDK types. The only distinguishing factors is, a component does not having its own lifecycle or define an identifier.

1.4. Collection types

A collection type refers to the data type itself, not its contents.

A Collection denotes a one-to-one or one-to-many relationship between tables of a database.

Refer to the chapter in collections for more information on collections.

2. Entity Types

Entities is application-specific classes which correlate to rows in a table, using a unique identifier. Because of the requirement for a unique identifier, ntities exist independently and define their own lifecycle. As an example, deleting a membership should not delete the User or the Group. For more information, see the chapter on persistent Classes.

From Hibernate official website

Hibernate Object Mapping Type

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.