Wind shadows summary nhibernate3 modelclass and Mapping

Source: Internet
Author: User
ArticleDirectory
    • Model class
Review above:

We have discussed how to configure the nhconfig file to connect to the database through NH.

The database is connected, but if you cannot perform crud operations, what should you do?

Summary:

This time, let's talk about model classes and corresponding ing files.

Model class

We all know that no matter what we do, we need to adopt a three-layer or multi-layer architecture to achieve decoupling and goal.CodeReduced coupling. NH aims to help us deal with the data access layer. To help me simplify the operation, SQL statements are all cloudification. After NH, the waist is no longer sour, and the legs are no longer painful. It is no longer difficult to go to the sixth floor in one breath ~~ Cough.

In fact, not only does NH help us simplify crud database operations, but there are also many frameworks available. For example, the latest version of Entity Framework in Microsoft seems to be 5 .. I used 4.2 for that moment. I'm old man!

Well, let's talk a little bit about it (a little bit--). We need to create the corresponding model Class Based on the table structure of the database.

This is the database table we created last time. We need to create the corresponding model Class Based on the table content.

You may find this a bit strange. A virtual keyword is displayed for each attribute in the model class. This keyword must be added to all database-related attributes in the NH convention. It looks strange. Another note is that we usually like to change the access modifier of the Set accessors of the ID attribute to protected. in this way, we can only read the ID, and the write data operation is completely handed over to NH, which will automatically inherit all your model classes.

This method is recommended by NH.

We also found that there is a set attribute in the model class.

We can use this set property to obtain all the relevant foreign key data (which needs to be configured in mapping ).

We also found the goodstype type attribute in the sub-Table class goods.

You can use these two navigation attributes to obtain the associated table data. (We need to configure in mapping) OH ~ Shengguang, this feature is worth looking forward!

Ing file mapping

Let's first look at the ing file of the main table.

As we said last time, we need to set this ing file as an embedded resource file,

Let's analyze the configuration of this file.

First, the root node of this configuration file is hibernate-mapping, and only one file can exist.

(1) Schema (optional): name of the data table structure;

(2) default-cascade (optional, default value: None): cascade type of the data table;

(3) Auto-import (optional, default value: True): indicates whether the class name can be not specified in the query statement;

(4) default-access (optional, default value: "property"): indicates the policy that nhib.pdf uses to access fields;

(5) assembly (optional): IfProgramWhen no class name prefix is specified, this option value is used as the default class name prefix;

(6) namespace (optional): indicates the same namespace;

(7) xmlns

Generally, we only need to set assembly, namespace, and xmlns.

Class Node

The child node of the hibernate-mappin node is a class node, which corresponds to tables and model classes in the database.

The name attribute specifies the model class. Note that name is composed of two parts. The first part is the namespace + Class Name of the corresponding model class. The second part is the set name. Separated by commas.

The table attribute is the corresponding table name in the database (that is, our table name and model class name can be different)

Full attribute list

(1) Name: The full name of the persistence class type in. NET and its assembly;

(2) Table: name of the data table in the database;

(3) discriminator-value (optional, the default value is the class name): used to distinguish individual Subsets and use with polymorphism;

(4) mutable (optional, default value: True): Specifies whether the class instance is variable;

(5) Schema (optional): Specify the schema option value in hibernate-mapping;

(6) proxy (optional): Specifies the interface used to implement the initial inertia. If all attributes are declared as virtual, you can specify the class name to this option;

(7) Dynamic-Update (optional, default value: false): indicates whether the update SQL statement generated by Nhibernate at runtime contains only modified fields;

(8) Dynamic-insert: (optional, default value: false) indicates whether the insert SQL statement generated during running of Nhibernate only contains fields whose values are not null;

(9) polymorphism (optional, default value: implict): Indicates whether there is a need to specify whether the query uses polymorphism;

(10) Where (optional): specifies where SQL statements to obtain objects of this class type;

(11) persister (optional): Specifies the customIclasspersister;

(12) lazy (optional): setting this item to true is equivalent to assigning its own class name to the proxy option.

Write it here. continue later.

Come here first--rewrite three times... Continue in the evening ..

.. Something happened last night--Well, let's continue.

Parameter Details

(1) Name: The full name of the persistence class type in. NET and its assembly;

(2) Table: name of the data table in the database;

(3) discriminator-value (optional, the default value is the class name): used to distinguish individual Subsets and use with polymorphism;

(4) mutable (optional, default value: True): Specifies whether the class instance is variable;

(5) Schema (optional): Specify the schema option value in hibernate-mapping;

(6) proxy (optional): Specifies the interface used to implement the initial inertia. If all attributes are declared as virtual, you can specify the class name to this option;

(7) Dynamic-Update (optional, default value: false): indicates whether the update SQL statement generated by Nhibernate at runtime contains only modified fields;

(8) Dynamic-insert: (optional, default value: false) indicates whether the insert SQL statement generated during running of Nhibernate only contains fields whose values are not null;

(9) polymorphism (optional, default value: implict): Indicates whether there is a need to specify whether the query uses polymorphism;

(10) Where (optional): specifies where SQL statements to obtain objects of this class type;

(11) persister (optional): Specifies the customIclasspersister;

(12) lazy (optional): setting this item to true is equivalent to assigning its own class name to the proxy option.

ID

The mapped class must declare an ID that corresponds to the primary key in the data table. ID is actually a property of the mapped class. Unlike the property declared using proerpty, the ID attribute value must be unique, you can use the ID to convert an object to a unique record in a data table, or convert a record in a data table to an object.

(1) name (optional): name of the attribute;

(2) type (optional): Type of the attribute;

(3) Column (optional, the default value is the same as the attribute name): name of the primary key field in the data table. If the User-Defined attribute name is the same as the field name, this option can be ignored;

(4) unsaved-value (optional) is a value that is appropriate (sensible) by default. A specific Identifier property value is used to indicate that the instance was just created and has not been saved. This can distinguish this instance from an instance loaded from the previous session (which may have been modified again-the Translator's note) but not persisted again.
(5) Access (optional-property by default): the policy that hibernate uses to access attribute values.

Generator

It's the most important thing.

This primary key generation policy

1) assigned
The primary key is generated by an external program and does not involve hibernate.

2) HiLo
Use hi/LoAlgorithmThe implementation of the primary key generation mechanism requires additional database tables to save the Historical Status of the primary key generation.

3) seqhilo
Similar to hilo, the primary key generation mechanism implemented through the HI/lo algorithm only stores the Historical Status of the primary key in sequence, which is suitable for databases that support sequence, such as oracle.

4) Increment
The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then each time the primary key needs to be generated
Add 1 as the primary key.
This method may cause a problem: if multiple instances access the same database, because each instance maintains the primary key status, different instances may generate the same
As a result, duplicate primary keys are abnormal. Therefore, if the same database has multiple instances to access, this method must be avoided.

5) Identity
Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.

6) sequence
Use the sequence mechanism provided by the database to generate the primary key. For example, sequence in oralce.

7) Native
Hibernate uses identity, Hilo, and sequence as the primary key generation method based on the underlying database.

8) UUID. HEX
The algorithm generated by hibernate Based on the 128-bit unique value generates a hexadecimal value (encoded with a 32-Bit String) as the primary key.

9) UUID. String
Similar to UUID. HEX, the generated primary key is not encoded (Length: 16 ). Problems may occur in some databases (such as PostgreSQL ).

10) Foreign
Use the field of the External table as the primary key.
Generally, using UUID. HEX to generate a primary key provides the best performance and database platform adaptability. (Recommended for versions earlier than nh3.3)

11) guid
Guid generated by calling system. guid. newguid.

12) guid. Comb
A random 10-byte guid, which combines the current date and time represented by six bytes to form a new guid. This algorithm can reduce index fragmentation while maintaining high performance. Highly recommended

13) guid. Native uses the guid mechanism in the database.

The next three are newly added primary key generation mechanisms.

Next session

I am too tired to write it here-the conversion war later will be written in Windows Live writer and will be published to the newly created Sina Blog and blog Park.

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.