首页 > 代码库 > Hibernate对象映射类型

Hibernate对象映射类型

Hibernate understands both the Java and JDBC representations of application data. The ability to read and write object data to a database is called marshalling, and is the function of a Hibernate type. A type is an 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 how to clone values.

Usage of the word type

A Hibernate type is 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 may refer to the Java type, the JDBC type, or the Hibernate type, depending on context.

1. Value types

1.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 specifications. You can override these mappings and provide and use alternative mappings. These topics are discussed further on.

Table 1.1. Basic Type Mappings

Hibernate typeDatabase typeJDBC typeType registry
org.hibernate.type.StringTypestringVARCHARstring, java.lang.String
org.hibernate.type.MaterializedClobstringCLOBmaterialized_clob
org.hibernate.type.TextTypestringLONGVARCHARtext
org.hibernate.type.CharacterTypechar, java.lang.CharacterCHARchar, java.lang.Character
org.hibernate.type.BooleanTypebooleanBITboolean, java.lang.Boolean
org.hibernate.type.NumericBooleanTypebooleanINTEGER, 0 is false, 1 is truenumeric_boolean
org.hibernate.type.YesNoTypebooleanCHAR, ‘N‘/‘n‘ is false, ‘Y‘/‘y‘ is true. The uppercase value is written to the database.yes_no
org.hibernate.type.TrueFalseTypebooleanCHAR, ‘F‘/‘f‘ is false, ‘T‘/‘t‘ is true. The uppercase value is written to the database.true_false
org.hibernate.type.ByteTypebyte, java.lang.ByteTINYINTbyte, java.lang.Byte
org.hibernate.type.ShortTypeshort, java.lang.ShortSMALLINTshort, java.lang.Short
org.hibernate.type.IntegerTypesint, java.lang.IntegerINTEGERint, java.lang.Integer
org.hibernate.type.LongTypelong, java.lang.LongBIGINTlong, java.lang.Long
org.hibernate.type.FloatTypefloat, java.lang.FloatFLOATfloat, java.lang.Float
org.hibernate.type.DoubleTypedouble, java.lang.DoubleDOUBLEdouble, java.lang.Double
org.hibernate.type.BigIntegerTypejava.math.BigIntegerNUMERICbig_integer
org.hibernate.type.BigDecimalTypejava.math.BigDecimalNUMERICbig_decimal, java.math.bigDecimal
org.hibernate.type.TimestampTypejava.sql.TimestampTIMESTAMPtimestamp, java.sql.Timestamp
org.hibernate.type.TimeTypejava.sql.TimeTIMEtime, java.sql.Time
org.hibernate.type.DateTypejava.sql.DateDATEdate, java.sql.Date
org.hibernate.type.CalendarTypejava.util.CalendarTIMESTAMPcalendar, java.util.Calendar
org.hibernate.type.CalendarDateTypejava.util.CalendarDATEcalendar_date
org.hibernate.type.CurrencyTypejava.util.CurrencyVARCHARcurrency, java.util.Currency
org.hibernate.type.LocaleTypejava.util.LocaleVARCHARlocale, java.utility.locale
org.hibernate.type.TimeZoneTypejava.util.TimeZoneVARCHAR, using the TimeZone IDtimezone, java.util.TimeZone
org.hibernate.type.UrlTypejava.net.URLVARCHARurl, java.net.URL
org.hibernate.type.ClassTypejava.lang.ClassVARCHAR, using the class nameclass, java.lang.Class
org.hibernate.type.BlobTypejava.sql.BlobBLOBblog, java.sql.Blob
org.hibernate.type.ClobTypejava.sql.ClobCLOBclob, java.sql.Clob
org.hibernate.type.BinaryTypeprimitive byte[]VARBINARYbinary, byte[]
org.hibernate.type.MaterializedBlobTypeprimitive byte[]BLOBmaterized_blob
org.hibernate.type.ImageTypeprimitive byte[]LONGVARBINARYimage
org.hibernate.type.BinaryTypejava.lang.Byte[]VARBINARYwrapper-binary
org.hibernate.type.CharArrayTypechar[]VARCHARcharacters, char[]
org.hibernate.type.CharacterArrayTypejava.lang.Character[]VARCHARwrapper-characters, Character[], java.lang.Character[]
org.hibernate.type.UUIDBinaryTypejava.util.UUIDBINARYuuid-binary, java.util.UUID
org.hibernate.type.UUIDCharTypejava.util.UUIDCHAR, can also read VARCHARuuid-char
org.hibernate.type.PostgresUUIDTypejava.util.UUIDPostgreSQL UUID, through Types#OTHER, which complies to the PostgreSQL JDBC driver definitionpg-uuid
org.hibernate.type.SerializableTypeimplementors of java.lang.SerializableVARBINARYUnlike the other value types, multiple instances of this type are registered. It is registered once under java.io.Serializable, and registered under the specific java.io.Serializable implementation class names.

1.2. National Character Types

National Character types, which is a new feature since JDBC 4.0 API, now available in hibernate type system. National Language Support enables you retrieve data or insert data into a database in any character set that the underlying database supports.

Depending on your environment, you might want to set the configuration option hibernate.use_nationalized_character_data to true and having all string or clob based attributes having this national character support automatically. There is nothing else to be changed, and you don‘t have to use any hibernate specific mapping, so it is portable ( though the national character support feature is not required and may not work on other JPA provider impl ).

The other way of using this feature is having the @Nationalized annotation on the attribute that should be nationalized. This 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 typeDatabase typeJDBC typeType registry
org.hibernate.type.StringNVarcharTypestringNVARCHARnstring
org.hibernate.type.NTextTypestringLONGNVARCHARmaterialized_clob
org.hibernate.type.NClobTypejava.sql.NClobNCLOBnclob
org.hibernate.type.MaterializedNClobTypestringNCLOBmaterialized_nclob
org.hibernate.type.PrimitiveCharacterArrayNClobTypechar[]NCHARchar[]
org.hibernate.type.CharacterNCharTypejava.lang.CharacterNCHARncharacter
org.hibernate.type.CharacterArrayNClobTypejava.lang.Character[]NCLOBCharacter[], java.lang.Character[]

1.3. Composite types

Composite types, or embedded types, as they are called by the Java Persistence API, have traditionally been called components in Hibernate. All of these terms mean the same thing.

Components represent aggregations of values into a single Java type. An example is an Address class, which aggregates street, city, state, and postal code. A composite type behaves in a similar way to an entity. They are each classes written specifically for an application. They may both include references to other application-specific classes, as well as to collections and simple JDK types. The only distinguishing factors are that a component does not have 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 on Collections for more information on collections.

2. Entity Types

Entities are 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.

 

摘自Hibernate官网

Hibernate对象映射类型