[Original]java Web Learning Note 81:hibernate Learning Path---Object relational mapping file (. hbm.xml): hibernate-mapping node, class node, ID node (primary key generation policy), property node

Source: Internet
Author: User
Tags db2

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1.POJO class and database mapping file *.hbm.xml hibernate mapping

 1) The mapping between the POJO class and the relational database can be defined with an XML document.

2) through the database mapping file of the POJO class, hibernate can understand the relationship between persisted classes and data tables , and can understand the correspondence between persisted class attributes and database table columns , how to Map primary keys , how to Map Association relationships

3) at runtime Hibernate will generate various SQL statements based on this mapping file

4) The mapping file has the extension. hbm.xml

2. Mapping File Description

Hibernate-mapping

Class hierarchy: Class

PRIMARY key: ID

Base type: Property

Entity reference class: Many-to-one | One-to-one

Collection: Set | List | Map | Array

One-to-many

Many-to-many

Sub-class: Subclass | Joined-subclass

Others: Component | Any etc.

Query statement: Queries (used to place query statements to facilitate unified management and optimization of database queries)

Multiple classes can be defined at the same time in each hibernate-mapping. However, it is more recommended to create a separate mapping file for each class

3. Detailed description of parameters

1) hibernate-mapping Node

      

2)class node

 3) ID node

    ①hibernate uses object identifiers (OIDs) to establish correspondence between objects in memory and records in database tables . The OID of the object corresponds to the primary key of the data table . Hibernate assigns a value to the primary key by using the identifier generator

②hibernate It is recommended that you use a proxy primary key in your data table, that is, a field that does not have business meaning. The surrogate primary key is usually an integer type because the integer type saves more database space than the string type.

③ in the object-relational mapping file, the <id> element is used to set the object identifier. The <generator> child element is used to set the identifier generator.

④hibernate provides the identifier generator interface: Identifiergenerator, and provides a variety of built-in implementations

      

1.increment Identifier Generatorby HibernateToIncrementAssign a value to the proxy primary key
①hibernate reads the maximum value of the primary key in the news table first, and then inserts a record into the news table, incrementing it on the basis of Max (ID), which is incremented by 1. ② Scope of application:Test> Because the increment survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems > for applications where only a single Hibernate application process accesses the same database, which is deprecated in a clustered environment > OIDs must is a long, int, or short type, and if the OID is defined as a byte type, an exception 2.identity identifier generator is thrown at run time
The ①identity identifier generator is responsible for generating identifiers by the underlying database.it requires the underlying database to define the primary key as the autogrow field type② Scope of application:Suitable for Db2/meysqlnot suitable for Oracle> Because the identity generation identifier mechanism relies on the underlying database system, it is required that the underlying database system must support the Autogrow field type. Databases that support autogrow field types include: DB2, Mysql, MSSQLServer, Sybase, etc. > OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

3.sequence Identifier Generator
  
①The sequence identifier generator uses the sequence provided by the underlying database to generate identifiers.
②hibernate When persisting a News object, first obtain a unique identification number from the NEWS_SEQ sequence of the underlying database, and then use it as the primary key value
③ Application scope: The underlying database supports sequence ORCLAE/DB2, not suitable for MySQL
> because the mechanism by which sequence generates identifiers relies on the sequence of underlying database systems, it is required that the underlying database system must support sequences. Supported sequences of databases include: DB2, Oracle, etc.
The > OID must be a long, int, or short type that throws an exception at run time if the OID is defined as a byte type

4.Hilo Identifier Generator
  
The ①hilo identifier generator is generated by Hibernate according to a high/low algorithm * that gets the high value from the field of a particular table in the database.
②hibernate When persisting a News object, Hibernate is responsible for generating the primary key value. when the Hilo identifier generator generates an identifier, it needs to read and modify the Next_value value in the Hi_table table .
③ Scope of application: applicable to all data, not dependent on the underlying database
> because the Hilo Survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems
The > OID must be a long, int, or short type that throws an exception at run time if the OID is defined as a byte type

5.Native Identifier Generator

The ①native identifier generator chooses to use the identity, sequence, or Hilo identifier generator, depending on the underlying database's ability to automatically generate identifiers .

② Scope of application: preferred this way

> Because native can automatically select the appropriate identifier generator based on the type of underlying database system, it is well suited for cross-database platform development

The > OID must be a long, int, or short type that throws an exception at run time if the OID is defined as a byte type

  

4) Property Node

    

  

  

  

[Original]java Web Learning Note 81:hibernate Learning Path---Object relational mapping file (. hbm.xml): hibernate-mapping node, class node, ID node (primary key generation policy), property node

Related Article

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.