首页 > 代码库 > 自学HIBERNATE5.0文档第一季之基础类型篇
自学HIBERNATE5.0文档第一季之基础类型篇
2.3. Basic Types
Basic value types usually map a single database column, to a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended by the JDBC specifications.
Internally Hibernate uses a registry of basic types when it needs to resolve a specific org.hibernate.type.Type
.
基础的值类型通常映射一个数据库列,到一个单个的,非聚合的java类型。HIbernate提供了大量的内置基础类型,遵循原生的映射被JDBC规范推荐。
当他需要去解析指定的org.hibernate.type.Type.时
在HIbernate内部使用一个基础类型的注册
2.3.1. Hibernate-provided BasicTypes(HIbernate提供的基础类型)
使用HIbernate-java8类型仅添加了HIbernate-java依赖到你的类路径,剩下的依赖HIbernate能够处理。
These mappings are managed by a service inside Hibernate called the org.hibernate.type.BasicTypeRegistry
, which essentially maintains a map oforg.hibernate.type.BasicType
(a org.hibernate.type.Type
specialization) instances keyed by a name. That is the purpose of the "BasicTypeRegistry key(s)" column in the previous tables.
通过一个服务内部的HIbernate调用 org.hibernate.type.BasicTypeRegistry管理这些映射,本质上维持一个映射的
org.hibernate.type.BasicType
(a org.hibernate.type.Type
specialization) 实例需要通过一个名字。这个目的的“BasicTypeRegistry key(s)”列里面之前的表。
2.3.2. The @Basic
annotation
Strictly speaking, a basic type is denoted with with the javax.persistence.Basic
annotation. Generally speaking, the @Basic
annotation can be ignored, as it is assumed by default. Both of the following examples are ultimately the same.
严格来说,一个基础的类型用javax.persistence.Basic注解来表示。通常来说,@Basic注解能够被忽略,因为他假设在默认的情况下,就像下面的例子最后都是一样的。
example 3. @Basic
declared explicitly
@Basic显示声明例子
@Entity(name = "Product") public class Product { @Id @Basic private Integer id; @Basic private String sku; @Basic private String name; @Basic private String description; }
Example 4. @Basic
being implicitly implied
含蓄的暗示
@Entity(name = "Product") public class Product { @Id private Integer id; private String sku; private String name; private String description; }
JPA规范严格的限制可标记为基础的java类型一下清单:
。。。
任何其他的类型要实现序列化(JPA的“支持”序列化的类型直接序列化它们到数据库)
如果提供者移植性是一个问题,你应该严格的去使用基础类型。注意JPA2.1已经添加javax.persistence.AttributeConverter的概念去帮助缓解这些问题。
The @Basic
annotation defines 2 attributes.
optional
- boolean (defaults to true)-
Defines whether this attribute allows nulls. JPA defines this as "a hint", which essentially means that it effect is specifically required. As long as the type is not primitive, Hibernate takes this to mean that the underlying column should be
NULLABLE
. fetch
- FetchType (defaults to EAGER)-
Defines whether this attribute should be fetched eagerly or lazily. JPA says that EAGER is a requirement to the provider (Hibernate) that the value should be fetched when the owner is fetched, while LAZY is merely a hint that the value be fetched when the attribute is accessed. Hibernate ignores this setting for basic types unless you are using bytecode enhancement. See the BytecodeEnhancement for additional information on fetching and on bytecode enhancement.
@Basic注解定义了两个三属性。
optional - boolean(默认为true)
定义这个属性是否为空值。JPA定义这个作为“一个示意”,本质的意思是它的影响是明确需要的。只要不是原始类型,HIbernate将它认为底层的列可以为空。
fetch-FetchType(默认为立即)
定义这个属性是否是立即加载或是懒加载。JPA说HIbernate提供者EAGER是必须当值被取得从这个所有物取得时,当访问这个属性时LAZY才调用这个值被取得。HIbernate忽略了设置基础类型,除非你使用字节码增强。
2.3.3. The @Column
annotation
JPA defines rules for implicitly determining the name of tables and columns. For a detailed discussion of implicit naming see Naming.
For basic type attributes, the implicit naming rule is that the column name is the same as the attribute name. If that implicit naming rule does not meet your requirements, you can explicitly tell Hibernate (and other providers) the column name to use.
JPA定义规则隐式地确定表名和列名。
基础类型属性,隐式命名规则中列名与属性名一样。如果隐式命名规则不能满足你的需要,你能够显示的告诉HIbernate(并且提供其他)列名给它使用。
Example 5. Explicit column naming(显示列名命名 例5)
@Entity(name = "Product") public class Product { @Id private Integer id; private String sku; private String name; @Column( name = "NOTES" ) private String description; }
Here we use @Column
to explicitly map the description
attribute to the NOTES
column, as opposed to the implicit column name description
.
The @Column
annotation defines other mapping information as well. See its Javadocs for details.
我们使用@Column显示地映射描述的属性去说明这个列名,作为相对于隐式列名的秒速。@Column注解也定义了其他的映射信息,查看Javadocs详情。。。
自学HIBERNATE5.0文档第一季之基础类型篇