Identifier generation

We have seen that generator was specified while mapping the identifier. The generator attribute is used to declare the algorithm to be used to generate an identifier value. Value used in the generator attribute is either the short name of algorithm that NHibernate provides out-of-the-box or the assembly qualified name of the NHibernate class that implements the said algorithm. NHibernate has implemented several such algorithms which should fit any identifier generation requirement. But if you are not happy with these then you can build your own algorithm by implementing interface NHibernate.Id.IIdentifierGenerator.

Let's take a look at important identifier generation algorithms that NHibernate offers out-of-the-box.

Identity

This algorithm works on the back of identity columns supported by SQL Server and MySQL. NH will save the entity without assigning any identifier value and database would generate the identifier values before actual database record is saved. NHibernate then retrieves the identifier generated by database and makes it available to application code that saved the entity.

Sequence

Similar to identity, this algorithm works in conjunction with sequences supported by Oracle, PostgreSQL, and SQL Server 2014.

Hilo

This implementation uses hi/lo algorithm to generate identifiers. Algorithm uses a high value retrieved from database and combines it with range of low values to generate a unique identifier. High value is retrieved from column next_hi of table hibernate_unique_key by default. But you can override this to use a different table. This algorithm also supports specifying a where parameter which can be used to retrieve high value for different entities from different rows of the hibernate_unique_key table. In Chapter 5, Let's Store Some Data into the Database, we will go over hilo algorithm in more detail.

Seqhilo

This implementation is similar to hilo. Only difference is that it uses a named database sequence as source of hi values. This strategy obviously only works for the databases that support sequences.

GUID

If the identifier property of a class is of System.Guid type then you can use the Globally Unique Identifier (GUID) generator to generate identifier values. This algorithm generates a new System.Guid for every record being inserted in the database.

Guid.comb

This is an improved generator implementation for identifiers of type System.Guid. In most relational databases, primary keys (identifiers in NHibernate world) are clustered and automatically indexed. System.Guid values are not index friendly. Hence, Jimmy Nilsson proposed a different mechanism to generate new System.Guid values that are index friendly and result in better performance. The Guid.comb generator implements algorithm proposed by Jimmy. You can read more about this algorithm at http://www.informit.com/articles/article.asp?p=25862.

Native

A native generator does not have its own algorithm. It picks up one of identity, sequence or hilo, depending on the capabilities of the underlying database.

Assigned

Similar to native, assigned does not have any algorithm to generate the identifier values. It is used to let NHibernate know that an application will assign the identifier values before entities are saved in the database.

We have only covered most important and widely used algorithms here. Note that there are more available out of the box. Among the ones presented here, hilo is most famous and results in most efficient database operations. We will see this in detail in Chapter 5, Let's Store Some Data into Database. For now, let's move on to the next topic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169