Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
63
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

Chapter 6.

Types

As an Object/Relational Mapping solution, Hibernate deals with both the Java and JDBC representations of application data. An online catalog application, for example, most likely has

Product object with a number of attributes such as a sku, name, etc. For these individual attributes, Hibernate must be able to read the values out of the database and write them back. This 'marshalling' is the function of Hibernate type, which is an implementation of the org.hibernate.type.Type interface. In addition, a Hibernate type describes various aspects of behavior of the Java type such as "how is equality checked?" or "how are values cloned?".

Important

A Hibernate type is neither a Java type nor a SQL datatype; it provides a information about both.

When you encounter the term type in regards to Hibernate be aware that usage might refer to the Java type, the SQL/JDBC type or the Hibernate type.

Hibernate categorizes types into two high-level groups: value types (see Section 6.1, “Value types”) and entity types (see Section 6.2, “Entity types”).

6.1. Value types

The main distinguishing characteristic of a value type is the fact that they do not define their own lifecycle. We say that they are "owned" by something else (specifically an entity, as we will see later) which defines their lifecycle. Value types are further classified into 3 sub-categories: basic types (see Section 6.1.1, “Basic value types”), composite types (see Section 6.1.2, “Composite types”) amd collection types (see Section 6.1.3, “Collection types”).

6.1.1. Basic value types

The norm for basic value types is that they map a single database value (column) to a single, nonaggregated Java type. Hibernate provides a number of built-in basic types, which we will present in the following sections by the Java type. Mainly these follow the natural mappings recommended in the JDBC specification. We will later cover how to override these mapping and how to provide and use alternative type mappings.

6.1.1.1. java.lang.String

org.hibernate.type.StringType

Maps a string to the JDBC VARCHAR type. This is the standard mapping for a string if no Hibernate type is specified.

Registered under string and java.lang.String in the type registry (see Section 6.5, “Type registry”).

143

Chapter 6. Types

org.hibernate.type.MaterializedClob

Maps a string to a JDBC CLOB type

Registered under materialized_clob in the type registry (see Section 6.5, “Type registry”).

org.hibernate.type.TextType

Maps a string to a JDBC LONGVARCHAR type

Registered under text in the type registry (see Section 6.5, “Type registry”).

6.1.1.2. java.lang.Character (or char primitive)

org.hibernate.type.CharacterType

Maps a char or java.lang.Character to a JDBC CHAR

Registered under char and java.lang.Character in the type registry (see Section 6.5, “Type registry”).

6.1.1.3. java.lang.Boolean (or boolean primitive)

org.hibernate.type.BooleanType

Maps a boolean to a JDBC BIT type

Registered under boolean and java.lang.Boolean in the type registry (see Section 6.5, “Type registry”).

org.hibernate.type.NumericBooleanType

Maps a boolean to a JDBC INTEGER type as 0 = false, 1 = true

Registered under numeric_boolean in the type registry (see Section 6.5, “Type registry”).

org.hibernate.type.YesNoType

Maps a boolean to a JDBC CHAR type as ('N' | 'n') = false, ( 'Y' | 'y' ) = true Registered under yes_no in the type registry (see Section 6.5, “Type registry”).

org.hibernate.type.TrueFalseType

Maps a boolean to a JDBC CHAR type as ('F' | 'f') = false, ( 'T' | 't' ) = true Registered under true_false in the type registry (see Section 6.5, “Type registry”).

6.1.1.4. java.lang.Byte (or byte primitive)

org.hibernate.type.ByteType

Maps a byte or java.lang.Byte to a JDBC TINYINT

Registered under byte and java.lang.Byte in the type registry (see Section 6.5, “Type registry”).

144

Basic value types

6.1.1.5. java.lang.Short (or short primitive)

org.hibernate.type.ShortType

Maps a short or java.lang.Short to a JDBC SMALLINT

Registered under short and java.lang.Short in the type registry (see Section 6.5, “Type registry”).

6.1.1.6. java.lang.Integer (or int primitive)

org.hibernate.type.IntegerTypes

Maps an int or java.lang.Integer to a JDBC INTEGER

Registered under int and java.lang.Integerin the type registry (see Section 6.5, “Type registry”).

6.1.1.7. java.lang.Long (or long primitive)

org.hibernate.type.LongType

Maps a long or java.lang.Long to a JDBC BIGINT

Registered under long and java.lang.Long in the type registry (see Section 6.5, “Type registry”).

6.1.1.8. java.lang.Float (or float primitive)

org.hibernate.type.FloatType

Maps a float or java.lang.Float to a JDBC FLOAT

Registered under float and java.lang.Float in the type registry (see Section 6.5, “Type registry”).

6.1.1.9. java.lang.Double (or double primitive)

org.hibernate.type.DoubleType

Maps a double or java.lang.Double to a JDBC DOUBLE

Registered under double and java.lang.Double in the type registry (see Section 6.5, “Type registry”).

6.1.1.10. java.math.BigInteger

org.hibernate.type.BigIntegerType

Maps a java.math.BigInteger to a JDBC NUMERIC

Registered under big_integer and java.math.BigInteger in the type registry (see

Section 6.5, “Type registry”).

145

Chapter 6. Types

6.1.1.11. java.math.BigDecimal

org.hibernate.type.BigDecimalType

Maps a java.math.BigDecimal to a JDBC NUMERIC

Registered under big_decimal and java.math.BigDecimal in the type registry (see

Section 6.5, “Type registry”).

6.1.1.12. java.util.Date or java.sql.Timestamp

org.hibernate.type.TimestampType

Maps a java.sql.Timestamp to a JDBC TIMESTAMP

Registered under timestamp, java.sql.Timestamp and java.util.Date in the type registry (see Section 6.5, “Type registry”).

6.1.1.13. java.sql.Time

org.hibernate.type.TimeType

Maps a java.sql.Time to a JDBC TIME

Registered under time and java.sql.Time in the type registry (see Section 6.5, “Type

registry”).

6.1.1.14. java.sql.Date

org.hibernate.type.DateType

Maps a java.sql.Date to a JDBC DATE

Registered under date and java.sql.Date in the type registry (see Section 6.5, “Type

registry”).

6.1.1.15. java.util.Calendar

org.hibernate.type.CalendarType

Maps a java.util.Calendar to a JDBC TIMESTAMP

Registered under calendar, java.util.Calendar and java.util.GregorianCalendar in the type registry (see Section 6.5, “Type registry”).

org.hibernate.type.CalendarDateType

Maps a java.util.Calendar to a JDBC DATE

Registered under calendar_date in the type registry (see Section 6.5, “Type registry”).

6.1.1.16. java.util.Currency

org.hibernate.type.CurrencyType

Maps a java.util.Currency to a JDBC VARCHAR (using the Currency code)

146

Соседние файлы в папке more