Self-study HIBERNATE5.0 document the first quarter of the basic type chapter

Source: Internet
Author: User

2.3. Basic Types

Basic value types usually map a single database column with a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended by the JDBC Specificati Ons.

Internally Hibernate uses a registry of basic types when it needs to resolve a specific org.hibernate.type.Type .

The underlying value type typically maps a database column to a single, non-aggregated Java type. Hibernate provides a number of built-in base types that follow native mappings that are recommended by the JDBC specification.

When he needs to parse the specified org.hibernate.type.Type.时 registry using one of the underlying types within hibernate

  

2.3.1. hibernate-provided basictypes (the underlying type provided by Hibernate)

Using the Hibernate-java8 type only adds Hibernate-java dependencies to your classpath, and the rest relies on hibernate to handle it.

These mappings is managed by a service inside Hibernate called org.hibernate.type.BasicTypeRegistry the, which essentially maintains a map of org.hibernate.type.BasicType (a specialization) instances keyed by a name. That's the purpose of the "Basictyperegistry key (s)" column in the previous tables.

Hibernate calls from within a serviceorg.hibernate.type.BasicTypeRegistry管理这些映射,本质上维持一个映射的org.hibernate.type.BasicType (a org.hibernate.type.Type specialization) 实例需要通过一个名字。这个目的的“BasicTypeRegistry key(s)”列里面之前的表。

2.3.2. The @BasicAnnotation

Strictly speaking, a basic type is denoted with with the javax.persistence.Basic annotation. Generally speaking, the @Basic annotation can be ignored, as it's assumed by default. Both of the following examples is ultimately the same.

Strictly speaking, a basic type is represented by a javax.persistence.Basic annotation. In general, @Basic annotations can be ignored because he assumes that, in the default case, as in the following example, the last is the same.

Example 3. @Basicdeclared explicitly

Example of @Basic Display declaration

  

@Entity (name = "Product")publicclass  product {    @Id    @Basic      Private  Integer ID;    @Basic    private  String SKU;    @Basic    private  String name;    @Basic    private  String description;}

Example 4. @Basicbeing implicitly implied

Implicit hints.

@Entity (name = "Product")publicclass  product {    @Id    Private Integer ID;     Private String SKU;     Private String name;     Private String description;}

The JPA specification strictly restricts the list of types of Java that can be marked as basic:

。。。

Any other type to be serialized (the type of JPA "support" serialization directly serializes them to the database)

If the provider portability is a problem, you should strictly use the underlying type. Note JPA2.1 has been addedjavax.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 the 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 are a requirement to the provider (Hibernate) that the value should being fetched when the owner is Fetche D, while LAZY was merely a hint that the value was fetched when the attribute was accessed. Hibernate ignores this setting for basic types unless is using bytecode enhancement. See the Bytecodeenhancement for additional information in fetching and on bytecode enhancement.

The @Basic annotations define two three properties.

Optional-boolean (default = True)

Defines whether this property is a null value. JPA defines this as "a sign", essentially meaning that its impact is clearly needed. As long as it is not of the original type, hibernate considers the underlying column to be empty.

Fetch-fetchtype (default is immediate)

Defines whether this property is immediately loaded or lazy loaded. JPA says that hibernate provider eager is required when the value is obtained from this property, and when this attribute is accessed, the lazy call is made. Hibernate ignores setting the underlying type unless you use bytecode enhancement.

2.3.3. The @ColumnAnnotation

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, 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.

The JPA definition rule implicitly determines the table name and column name.

The underlying type property, which has the same column name as the property name in an implicit naming rule. If the implicit naming convention does not meet your needs, you can show it to hibernate (and provide other) column names for it to use.

Example 5. Explicit column naming (show listing name Example 5)

@Entity (name = "Product")publicclass  product {    @Id    Private Integer ID;     Private String SKU;     Private String name;     = "NOTES" )    Private  String description;}

Here we use @Column to explicitly maps description NOTES the attribute to the column, as opposed to the implicit column name .

The @Column annotation defines other mapping information as well. See it Javadocs for details.

We use the properties of the @column display map description to describe the column name as a second speed relative to the implicitly column name. @Column annotations also define other mapping information, view javadocs details ...

Self-study HIBERNATE5.0 document the first quarter of the basic type chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.