Primary Key Specifics

It is possible to have three types of primary keys in Entity Developer:

Simple primary key (in NHibernate terminology: id). To define a property as a simple primary key, select the Primary Key check box in the Property Editor dialog. For simple primary keys, you can select a generator from the Generator drop-down list.
Composite primary key (in NHibernate terminology: composite key). It is possible to have more than one primary key property within a class.
Complex type composite primary key (in NHibernate terminology: Components as composite identifiers). You can create a complex type consisting of class's properties and then define the resulting complex type property as a primary key.

Entity Developer for NHibernate provides a special mechanism for numeric sequence generator. Generators are used for generating number sequences for key fields. There are a few types of generators like increment, identity, sequence, hilo, etc. Some of them generate simple numbers that are based on DB sequences and identity fields, and others generate identifiers based on special algorithms. Generators are not used for composite and complex type composite primary keys.

Below is the description of generators available in the Generator drop-down list:

ExpandedToggleIcon        Primary Key Generators

Generator

Description

Default

The generator used for the whole model. Note that default generator for all properties in the model can be defined by selecting the appropriate value in the Default Identity Generator line of the model's properties and can be redefined for any specific property.

Assigned

Lets the application assign an identifier to the object before Save() is called.

Foreign

Uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.

Guid

Uses a new System.Guid as the identifier.

Guid.comb

("combined guid/timestamp") replaces the last 6 bytes of Data4 with the least-significant 6 bytes of the current system date/time.

Guid.native

Allows using the RDBMS native function to generate GUID. The behavior is similar to the “sequence” generator: when a new object is saved, NH runs two queries; the first one to retrieve the GUID value and the second one to insert the entity using the GUID retrieved from the RDBMS.

Hilo

Uses a hi/lo algorithm to efficiently generate identifiers of any integral type, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with a user-supplied connection. You can use the "where" parameter to specify the row to use in a table. This is useful if you want to use a single label for your identifiers, with different rows for each table.

Identity

Supports identity columns in DB2, MySQL, MS SQL Server and Sybase. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

Increment

Generates identifiers of any integral type that are unique only when no other process is inserting data into the same table.

Native

Picks identity, sequence or hilo depending upon the capabilities of the underlying database.

Select

Retrieves a primary key assigned by a database trigger. The trigger selects some unique row value and retrieves the primary key value.

Seqhilo

Uses a hi/lo algorithm to efficiently generate identifiers of any integral type, given a named database sequence.

Sequence

Uses a sequence in DB2, PostgreSQL, Oracle or a generator in Firebird. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

Sequence-identity

Is based on “sequence” but works as an “identity”. The POID value is retrieved with the INSERT query. The type of an entity can be System.Int32 or System.Int64 depending on the RDBMS sequence generator.

Trigger-identity

Is a NHibernate specific feature where the POID is generated by the RDBMS at the INSERT query through a BEFORE INSERT trigger. In this case you can use any supported type, including custom type, with the limitation of “single-column”.

Uuid.hex

Uses System.Guid and its ToString(string format) method to generate identifiers of the string type. The length of the string returned depends on the configured format.

Uuid.string

Uses a new System.Guid to create a byte[] that is converted to a string.

Custom

User-defined primary key generator.

 

ExpandedToggleIcon        Unsaved Value

It is a property value which indicates that an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.

note Note

The Unsaved value field is available only for primary key or version properties (for them the Version property is set to True).

The allowed values of unsaved value are:

any - always save.

none - always update.

null - save when identifier is null.

valid identifier value - save when identifier is null or equals the user-defined value.

undefined - if set for version or timestamp, then identifier check is used.

If unsaved value is not specified for a class, NHibernate will make an attempt to guess it by creating an instance of the class using the no-argument constructor and reading the property value from the instance.

 


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.