My OR Mapping Tour (2)

xiaoxiao2021-03-06  69

In the previous part I only talk about how to generate mapping files and Pojo through the form in the database. In this section, I will tell the mapping file.

Let's take a look at this table:

In a real-world vehicle management system, you will never place the vehicle information and the owner information in a table. It should be "an owner" to have one or more "vehicles". Let's take a look at the decomposed table, the people table, set the Owner_ID as the primary key:

Auto_INFO table, set auto_id as the primary key:

Configure the main, foreign key relationship for two tables, and set the PEOPLE table as the main table:

Configure the main, foreign key relationship for two tables, and set the PEOPLE table as the main table:

Configure the main, foreign key relationship for two tables, and set the PEOPLE table as the main table:

Open Eclipse to generate a mapping file for these two tables.

AutoInfo.hbm.xml:

people.hbm.xml:

Analysis by autoco.hbm.xml:

DOCTYPE defines this XML file specification DTD, which can be obtained by rear URL, or get it in Hibernate's ClassPath. Hibernate-mapping package = "bo" defines the package "bo" existing in Pojo.

Class name = "AutoInfo" Table = "auto_info" defines the name "autoinfo" and the table name "Auto_Info" in the database of Pojo.

ID column = "auto_id" name = "id" type = "integer" defines the primary key field in the database table, contains unique identifiers for each instance of the corresponding POJO. Column is a database table field name, a name identifier attribute name, Type description in POJO. In addition, there is an important identification: Unsaved-value will be used later.

Generator provides unique identifiers for each Pojo instance. In general, we use "native". Class represents an instance implemented by the generator interface NET.SF.HIBERNATE.ID.IDENTIFIERGENERATOR, including:

Assigned "

The primary key is generated by the external program and specifies one before save (). "Hilo"

The primary key generation mechanism implemented by the Hi / LO algorithm requires additional database tables or fields to provide high-bit values.

"Seqhilo"

Similar to HILO, primary key generation mechanism implemented by Hi / LO algorithm, requires Sequence in the database, which is suitable for supporting Sequence's database, such as Oracle.

"Increment"

The primary key is incremented in the order of value. The implementation mechanism of this manner is to maintain a variable in the current application instance to save the current maximum, and then add this value to 1 as the primary key each time you need to generate a primary key. The problem that may be generated in this way is that it cannot be used under the cluster.

Identity "

The primary key generation mechanism provided by the database. Such as the primary key generating mechanism in DB2, SQL Server, MySQL.

"Sequence"

The SEQUENCE mechanism supplied with the database generates a primary key. Such as Sequence in Oralce.

"Native"

One of the use of Identity, Hilo, Sequence is used as a primary key generation by Hibernate.

"Uuid.hex"

The 16-based value is generated based on the 128-bit UUID algorithm based on the 128-bit UUID algorithm (indicated by a string of length 32) as the primary key.

UUID.STRING

Similar to uuid.hex, only the generated primary key is not encoded (length 16), and cannot be applied in the PostgreSQL database.

"ForeIgn"

Use another identifier of another associated object as the primary key. property column = "LICENSE_PLATE" length = "20" name = "LicensePlate" not-null = "false" type = "string" property column = "LICENSE_PLATE" length = "20" name = "LicensePlate" not-null = "false "Type =" string "proty color column =" license_plate "length =" 20 "name =" licenseplate "not-null =" false "type =" string "

Define the properties in POJO, indicating the fields, lengths, POJO attribute names in the database table, and type, non-empty state. Many-to-one class = "people" name = "ownerno" not-null = "true" column name = "oowner_no"

A persistent object is defined to the relationship between another persistent object, which is more related to one. Applying in the vehicle management system, a number of vehicles are owned by one owner. The name of the associated class is indicated, respectively, and the property name, non-empty state, and field name.

After analyzing autoinfo.hbm.xml, take a look at People.hbm.xml. Most of the content has appeared in front, different places are:

In the vehicle management system, it represents a number of vehicles with an owner. Expressed in java.util.set type. INVERSE is used to identify one end of the passive party in the two-way association. Inverse = false side is responsible for maintaining the relationship; in the vehicle management system, AutoInfo acts as the main control, should be set to "True". This is like you (passive One) a lot of cards in a party, but it is possible that you don't know the specific background of the recipient (active MANY); this is not tight, the recipient will contact you when necessary. Active maintenance relationship).

In addition, there is an important identification in the attribute of the Set node: Cascade relationship, indicating which operations (INSERT, UPDATE, DELETE) will be associated with associated objects from the host object level.

Optional value:

All: save (), saveorupdate (), Update (), delete () all grade operation.

None: Class operation is not performed in all cases.

Save-Update: Level operation when performing save (), saveorupdate (), update ().

DELETE: Classlion only when delte () is executed.

Cascade is a very important concept in the Hibernate mapping relationship. It refers to whether the associated object (passive) is simultaneously saving the associated object (passive) when the host object invokes the Save-Update or the Delete method. In this mapping file, when the owner is updated or deleted, the vehicle-associated vehicle (AutoInfo) can be modified or deleted, so the cascading relationship should be set to cascade = "all".

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-88692.html

New Post(0)