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 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‘/‘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 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 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 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官网

时间: 2024-10-13 17:40:50

Hibernate对象映射类型的相关文章

hibernate的映射类型

hibernate的映射类型 hibernate MySQL映射类型 1.Hibernate的映射类型 hibernate mysql映射类型 Hibernate 映射类型 Java 类型 标准 SQL 类型 大小和取值范围 integer 或者 int int 或者 java.lang.Integer INTEGER 4 字节 long long  Long BIGINT 8 字节 short short  Short SMALLINT 2 字节 byte byte  Byte TINYINT

攻城狮在路上(壹) Hibernate(九)--- Hibernate的映射类型

Hibernate采用映射类型作为Java类型和SQL类型的桥梁,对应type属性.分为两种:内置映射类型和客户化映射类型.一.内置映射类型: 1.Java基本类型的Hibernate映射类型: Java基础类型的Hibernate映射类型 Hibernate映射类型 Java类型 标准SQL类型 大小和取值范围 integer或者int int或者java.lang.Integer INTEGER   long long BIGINT   short short SMALLINT   byte

hibernate教程—映射类型

Hibernate映射类型分为两种:内置映射类型和客户化映射类型.内置映射类型负责把一些常见的Java类型映射到相应的SQL类型:此外,Hibernate还允许用户实现UserType或CompositeUserType接口,来灵活地定制客户化映射类型.客户化类型能够把用户定义的Java类型映射到数据库表的相应字段. 一.Hibernate的内置映射类型 1.Java基本类型的Hibernate映射类型 Hibernate映射类型 Java类型 标准SQL类型 大小和取值范围 integer或者

Hibernate对象映射关系:一对一

一:项目截图 二:业务代码 package com.cloud.domain; import java.util.Date; public class IdCard { private Integer id; private Date validateDte; private Person person; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Dat

Hibernate对象映射关系:多对一

一:项目截图 二:业务代码 package com.cloud.domain; import java.util.Set; public class Department implements java.io.Serializable{ /** * 实现java.io.Serializable 接口的类是可序列化的. * 没有实现此接口的类将不能使它们的任一状态被序列化或逆序列化. */ private static final long seriaVersionUID = 1L; privat

Hibernate日期映射类型

映 射 类 型 Java类型 标准SQL类型 描    述 date java.util.Date或者java.sql.Date DATE 代表日期,形式为: YYYY-MM-DD time java.util.Date或者java.sql.Time TIME 代表时间,形式为: HH:MM:SS timestamp java.util.Date或者java.sql. Timestamp TIMESTAMP 代表时间和日期, 形式为: YYYYMMDDHHMMSS calendar java.u

3.Hibernate 映射类型

映射类型 当你准备一个 Hibernate 映射文件时,我们已经看到你把 Java 数据类型映射到了 RDBMS 数据格式.在映射文件中已经声明被使用的 types 不是 Java 数据类型:它们也不是 SQL 数据库类型.这种类型被称为 Hibernate 映射类型,可以从 Java 翻译成 SQL,反之亦然. 在这一章中列举出所有的基础,日期和时间,大型数据对象,和其它内嵌的映射数据类型. 原始类型 映射类型 Java 类型 ANSI SQL 类型 integer int 或 java.la

hibernate 对象关系映射文件详解

POJO 类和数据库的映射文件*.hbm.xml POJO类和关系数据库之间的映射可以用一个XML文档来定义. 映射文件的扩展名为.hbm.xml 在运行时Hibernate将根据这个映射文件来生成各种SQL语句 通过POJO类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对应关系 映射文件说明 hibernate-mapping 类层次:class 主键:id 基本类型:property 实体引用类: many-to-one

hibernate之实体类型到映射文件

1.通过写hibernate的映射文件,将实体类型转换成数据库中的表 其中那个映射文件是根据实体类型而写的. 实体类型User.java package cn.wwh.www.hibernate.dd.property; import java.util.Arrays; import java.util.Date; /** *类的作用: * * *@author 一叶扁舟 *@version 1.0 *@创建时间: 2014-8-17 下午08:05:30 */ public class Use