Detailed description of hibernate object link ing File

Source: Internet
Author: User

POJO class and database ing file *. hbm. xml

The poing between POJO classes and relational databases can be defined in an XML document. The extension of the ing file is. hbm. at runtime, xml Hibernate will generate various SQL statements based on the ing file and use the POJO database ing file. Hibernate can understand the correspondence between persistence classes and data tables, you can also understand the correspondence between persistence attributes and database columns.
Ing file description
Hibernate-mapping

Class Level: class

Primary Key: id basic type: property
Object Reference class: Allow-to-one | one-to-one
Set: set | list | map | array one-to-least
Copying-to-Sequence

Subclass: subclass | joined-subclass other: component | any and other query statements: query (used to place query statements to facilitate unified management and optimization of database queries)

Multiple classes can be defined in each Hibernate-mapping. However, we recommend that you create a separate ing file for each class.


Hibernate-mapping


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Y2xhc3M8YnI + cda-vcd4kpha + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090354267.png" alt = "\">



Ing object identifier

Hibernate uses the object identifier (OID) to establish the correspondence between objects in the memory and records in the database table. The OID of the object corresponds to the primary key of the data table. Hibernate uses the identifier generator to assign values to the primary key.
Hibernate recommends that you use the proxy primary key in the data table, that is, fields that do not have business meanings. The proxy primary key is usually of the integer type, because the integer type saves more database space than the string type.
In the object-link ing file, Element is used to set the object identifier. The child element is used to set the identifier generator.
Hibernate provides the identifier Generator interface: IdentifierGenerator, and provides various built-in implementations.

Id




Generator




Generator

Built-in identifier generator provided by Hibernate:


Increment identifier Generator

The increment identifier generator is assigned an increment value for the proxy primary key by Hibernate.
Hibernate will first read the maximum value of the primary key in the NEWS table, and then insert records into the NEWS table, it will increase progressively based on max (id), the increment is 1.
Applicability:

Because the increment survival identifier mechanism does not depend on the underlying database system, it is suitable for All Database Systems
This method is applicable to scenarios where only a single Hibernate application process accesses the same database. It is not recommended in a cluster environment and is only used for testing development environments. It is only applicable to single-process development environments.
The OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running.

Identity identifier Generator
The identity identifier generator is used by the underlying database to generate identifiers. It requires the underlying database to define the primary key as an Automatically increasing field type.
Applicability:
Because the identity generation mechanism depends on the underlying database system, the underlying database system must support automatic field growth. databases that support automatic field growth include: DB2, Mysql, MSSQLServer, Sybase, etc.
The OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running.

Sequence identifier Generator
The sequence identifier generator uses the sequence provided by the underlying database to generate the identifier.

When Hibernate persists a News object, it first obtains a unique ID number from the news_seq sequence of the underlying database, and then uses it as the primary key value.
Applicability:
Because the sequence generation mechanism of sequence depends on the sequence of the underlying database system, it is required that the underlying database system must support the sequence. Databases supporting the sequence include DB2 and Oracle.
The OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running.

Hilo identifier Generator
The hilo identifier generator is generated by Hibernate based on a high/low algorithm *. It obtains the high value from the fields of a specific table in the database.

When Hibernate persists a News object, Hibernate is responsible for generating the primary key value. When the hilo identifier generator generates the identifier, it needs to read and modify the NEXT_VALUE value in the HI_TABLE table.
Applicability:
Because the hilo survival identifier mechanism does not depend on the underlying database system, it is suitable for all database systems.
The OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running.

The hilo and seqhilo generators provide the first implementation of two hi/lo algorithms:
   
    
     Zhxy_hilo_tbl
     Next_value
     100
    
   In the second case, sequence is required. Here we only discuss the more common first case. By default, the table used is hibernate_unique_key. The default field is next_hi. Next_hi must have a record or an error may occur. Several shorthand explanations: hi: High Value-the value obtained from the database lo: low value-automatically maintained by hibernate; values: 1 to max_lowmax_low: the value configured in the ing file. How does hibernate generate the primary key? 1. obtain the hi value from the database, and add 12 to the next_value of the database. hibernate gets the lo value (0 to the max_lo-1 loop, lo to max_lo, execute step 1, then lo continues from 0 to the max_lo-1 loop) calculated based on the following formula: hi * (max_lo + 1) + lo; for example, if hi is initially set to 2 and max_lo is set to 3, the values generated are: Read hi is set to 2 and write to database is set to 32*(3 + 1) + 0 = 82*(3 + 1) + 1 = 92*(3 + 1) + 2 = 102*(3 + 1) + 3 = 11 this read/write table zhxy_hilo_tbl operation, the hi changes to 3, the database becomes 43*(3 + 1) + 0 = 123*(3 + 1) + 1 = 13 shut down the database. When the database is opened next time, the read hi value is 4, and the database changes to 54*(3 + 1) + 0 = 16, but there is a special situation, that is, when the hi value is 0, the first value is not 0 * (max_lo + 1) + 0 = 0, but skipped 0, directly 1

Native identifier Generator
The native identifier generator uses the identity, sequence, or hilo identifier generator based on the ability of the underlying database to automatically generate identifiers.
Applicability:
Because native can automatically select an identifier generator based on the type of the underlying database system, it is suitable for cross-database platform development.
The OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running.

Property




Mappings between Java, Hibernate, and SQL types






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.