Detailed explanation of *.hbm.xml mapping file

The mapping file is used to  provide Hibernate  with information about persisting objects into the relational database. The structure of each mapping file is basically the same. The sample code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!--dtd constraint information of the mapping file-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- name represents the complete class name (including the class name), table represents the table name -->
    <class name="className" table="tableName">
        <!-- name represents the unique identification attribute in the className class, column represents the primary key id in the tableName table -->
        <id name="id" column="id">
            <!-- primary key generation strategy -->
            <generator class="native" />
        </id>
        <!-- name indicates the common attribute of className column indicates the common field type of the tableName table indicates the field type -->
        <property name="attrName" column="fieIdName" type="string" />
    </class>
</hibernate-mapping>

In the above code, the xml declaration is first made, and then the dtd information of the mapping file is defined. Readers of this dtd information do not need to write it by hand. You can find the core JAR of hibernate in the Web App Libraries directory (or Referenced Libraries directory) of the project. Package hibernate3.jar, after opening this JAR package, you can find the hibernate-mapping-3.0.dtd file in the org.hibernate package.

After opening this file, there is this dtd information at the top of the file, just copy this dtd information to the mapping file for use. The dtd information in the hibernate-mapping-3.0.dtd file is shown in Figure 1.
 

dtd information of the mapping file


Figure 1 dtd information of the mapping file


Below the dtd information of the above mapping file code is the specific configuration of Hibernate mapping. The common attributes of each element in its configuration will be explained in detail below.

1. <hibernate-mapping> element

The <hibernate-mapping> element is the root element of the mapping file, and the attributes defined by it are valid in all nodes of the mapping file. The common attributes contained in the <hibernate-mapping> element and their meanings are shown in Table 1.
 

Table 1 Commonly used attributes of the <hibernate-mapping> element and their descriptions
attribute name Is it necessary illustrate
package no Specify a package prefix for classes in the mapping file, for use with unqualified class names
schema no Specify the database schema name
catalog no Specify the database catalog name
default-access no  Specify the strategy used by Hibernate to access properties, the default is property. When default-access="property", use getter and setter methods to access member variables; when default-access = "field", use reflection to access member variables
default-cascade no  Specifies the default cascading style, default is empty
default-lazy no Specify the lazy loading strategy adopted by Hibernate by default, the default is true

2. The <class> element

The <class> element is mainly used to specify the mapping relationship between persistent classes and data tables, and it is the main configuration content in the XML configuration file. Its common attributes and their meanings are shown in Table 2.

Table 2 Commonly used attributes of the <class> element and their descriptions
attribute name Is it necessary illustrate
name no The fully qualified name of the persistent class or interface. If this property is not defined, Hibernate treats the map as a map of non-POJO entities
table no The database table name corresponding to the persistent class, which defaults to the unqualified class name of the persistent class
catalog no Database catalog name, if specified, will override the catalog attribute value specified in the hibernate-mapping element
lazy no Specifies whether to use lazy loading

3. <id> element

The <class> element contains an <id> element, which is used to set the mapping between the OID (Object identifier) ​​of the persistent class and the primary key of the table. The common attributes and their meanings are shown in Table 3 Show.

Table 3 Commonly used attributes of the <id> element and their descriptions
attribute name Is it necessary illustrate
name no The property name that identifies the OID of the persistent class
type no The data type of the identification property in the persistent class. If no mapping type is explicitly set for a property, Hibernate will use the reflection mechanism to first identify the  Java  type of the specific property of the persistent class, and then automatically use the corresponding default Hibernate mapping type.
Java's basic data types and packaging types correspond to the same Hibernate mapping type, and basic data types cannot express null, so for the OID of persistent classes, it is recommended to use packaging types (integer, long, string, etc.)
column no Set the column name of the data column mapped to the identity attribute (the name of the primary key field)
access no Specifies Hibernate's access strategy for identifying attributes, defaulting to property. If this attribute is specified here, it will override the default-access attribute specified in the <hibemate-mapping> element


In addition to the above five elements, the <id> element can also contain an optional child element <generator>.

The <generator> element specifies how the primary key is generated. For different relational databases and business applications, the primary key generation methods are often different. Some rely on database auto-increment fields to generate primary keys, and some are determined according to specific application logic. These different implementations can be specified through the <generator> element Way. These implementations are also known as primary key generation strategies in Hibernate.

Before explaining Hibernate's primary key generation strategy, you need to understand two concepts: natural primary key and surrogate primary key. A description of these two concepts follows.

1) Natural primary key

A field with business meaning is used as a primary key, which is called a natural primary key. For example, in the user table, if the name field is used as the primary key, the prerequisites must be: the name of each user is not allowed to be null, the user is not allowed to have the same name, and the user name is not allowed to be modified.

Although these are feasible, they cannot meet the ever-changing business needs. Once there is a business need to allow users to have the same name, the data model must be modified and the primary key of the table must be redefined, which increases the difficulty of database maintenance.

2) Surrogate primary key

Using a field that does not have business meaning as a primary key is called a surrogate primary key. This field is generally named ID, which is usually an integer type, because the integer type can save more database space than the string type. In the above example, it is obviously more reasonable to use a surrogate primary key.

In Hibernate, several built-in primary key generation strategies are provided, and the names and descriptions of common primary key generation strategies are shown in Table 4.
 

Table 4 primary key generation strategy
Name describe
increment For long.short or int type, the unique identifier is automatically generated by Hibernate in increments, each increment is 1. It can only be used when no other process inserts data into the same table. It cannot be used in a cluster environment. It is suitable for proxy primary keys
identity Identifiers are generated using the primary key provided by the underlying database itself, provided the database supports auto-increment data types. This generator can be used in DB2, MySQL , SQL Server, Sybase and HypersonicSQL databases. This generator requires that the primary key be defined as an auto-increment type in the database, and is suitable for surrogate primary keys.
sequence Hibernate generates identifiers based on the underlying database sequence. The condition is that the database supports sequences, suitable for surrogate primary keys
hilo Efficiently generate identifiers of type long, short, or int using a high/low algorithm. Given a table and field (the default table and field are hibernate_unique_key and next_hi) as the source of high value. Identifiers produced by the high/low algorithm are only unique within a particular database
native  According to the ability of the underlying database to automatically generate identifiers, choose one of the three generators: identity, sequence, and hilo, suitable for cross-database platform development, and suitable for proxy primary keys
uuid Hibernate uses the 128-bit UUID algorithm to generate identifiers. This algorithm is capable of generating unique string identifiers in a network environment, whose UUID is encoded as a 32-bit hexadecimal string. This strategy is not popular because string-type primary keys take up more database space than integer-type primary keys and are suitable for surrogate primary keys
assigned The java program is responsible for generating the identifier. If the generator attribute of the id element is not specified, the primary key generation strategy will be used by default, which is suitable for natural primary keys

4) <property> element

The <class> element can contain multiple <property> sub-elements, which are used to indicate the mapping relationship between other attributes of the persistent class and non-primary key fields in the data table. The common attributes contained in the <property> element and their meanings are shown in Table 5.

Table 5 Common attributes of <property> element
attribute name illustrate
name The name of the persistent class property, starting with a lowercase letter
column Data table field name
type Data table field type
length The length of the data table field definition
lazy Specifies whether to use lazy loading for this property when the instance of the persistent class is accessed for the first time. The default value is false
unique Whether to generate a unique constraint on the mapped column. Often used when generating DDL statements or creating database objects
not-null Whether to allow mapping columns to be empty

 

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132096084
Recommended