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官网
Hibernate对象映射类型的更多相关文章
- hibernate的映射类型
hibernate的映射类型 hibernate MySQL映射类型 1.Hibernate的映射类型 hibernate mysql映射类型 Hibernate 映射类型 Java 类型 标准 SQ ...
- 攻城狮在路上(壹) Hibernate(九)--- Hibernate的映射类型
Hibernate采用映射类型作为Java类型和SQL类型的桥梁,对应type属性.分为两种:内置映射类型和客户化映射类型.一.内置映射类型: 1.Java基本类型的Hibernate映射类型: Ja ...
- Hibernate逍遥游记-第9章 Hibernate的映射类型
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate日期映射类型
映 射 类 型 Java类型 标准SQL类型 描 述 date java.util.Date或者java.sql.Date DATE 代表日期,形式为: YYYY-MM-DD time java ...
- Hibernate基本映射类型
- [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate对象的状态和映射
一. Hibernate对象的状态 实体对象的三种状态: 1) 暂态(瞬时态)(Transient)---实体在内存中的自由存在,它与数据库的记录无关. po在DB中无记录(无副本),po和sessi ...
- Hibernate 对象关系映射文件
简介: POJO 类和关系型数据库之间的映射可以用一个 XML 文档来定义 通过 POJO 类的数据库映射文件,Hibernate 可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据 ...
- hibernate字段映射枚举类型
上一篇介绍了mybatis字段映射枚举类型,这一篇给大家介绍一下hibernate字段怎么去映射枚举类型的(这只是一种参考方式,映射方法还有很多种). 还是以上篇sku表为例,sku表里一个statu ...
随机推荐
- OpenGL的GLUT初始化函数[转]
OpenGL的GLUT初始化函数 void glutInit(int* argc,char** argv) 初始化GLUT库.对应main函数的形式应是:int main(int argc,char* ...
- RTB
RTB —— Real Time Bidding 的简称,就是实时竞价.跟传统购买形式相比,RTB是在每一个广告展示曝光的基础上进行竞价,就是每一个PV都会进行一次展现竞价,谁出价高,谁的广告就会被这 ...
- Html表单的正则校验--将不符合指定正则表达式的字符串自动替换为空
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Hexo+github 搭建个人博客(一)
一.软件环境准备 1.安装git windows下载exe安装:linux 执行 apt-get install git-core 安装 2.安装Node.js windows使用 msi 文件进行安 ...
- HTML5游戏实战(4): 20行代码实现FlappyBird
这个系列很久没有更新了.几个月前有位读者调侃说,能不能一行代码做一个游戏呢.呵呵,接下来一段时间,我天天都在想这个问题,怎么能让GameBuilder+CanTK进一步简化游戏的开发呢.经过几个月的努 ...
- linux笔记:linux常用命令-关机重启命令
关机重启命令:shutdown(关机或者重启) 其他关机命令: 其他重启命令: 系统运行级别: 修改系统默认运行级别和查询系统运行级别: 退出登录命令:logout(退出登录)
- 可持久化Trie & 可持久化平衡树 专题练习
[xsy1629]可持久化序列 - 可持久化平衡树 http://www.cnblogs.com/Sdchr/p/6258827.html [bzoj4260]REBXOR - Trie 事实上只是一 ...
- 带不带protype的区别
总结写在前面: ①:带有protype:表示类的扩展,必须new后才能使用. ②:不带protype:属于静态方法,直接调用即可. html代码: <!DOCTYPE html> < ...
- JavaScript学习笔记(十二) 回调模式(Callback Pattern)
函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...
- robotframework笔记11
测试用例的语法 基本语法 测试用例构造测试用例表中可用 关键词. 关键字可以进口 测试库 或 资源 文件 或创建的 关键字表 的测试用例文件 本身. 测试用例表中第一列包含测试用例的名称. 一个 测试 ...