NHibernate

This article is about the .NET and Mono library. For other uses, see Hibernate.
NHibernate
Stable release
4.0.4 GA / August 17, 2015 (2015-08-17)
Written in C#
Operating system Cross-platform
Platform .NET 1.1-4.6 and Mono
Type Object-relational mapping
License GNU Lesser General Public License
Website nhibernate.info

NHibernate is an object-relational mapping (ORM) solution for the Microsoft .NET platform. It provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks. NHibernate is free as open source software that is distributed under the GNU Lesser General Public License. NHibernate is a port of Hibernate.

Feature summary

NHibernate's primary feature is mapping from .NET classes to database tables (and from CLR data types to SQL data types). NHibernate also provides data query and retrieval facilities. NHibernate generates the SQL commands and relieves the developer from manual data set handling and object conversion, keeping the application portable to most SQL databases, with database portability delivered at very little performance overhead.

NHibernate provides transparent persistence for Plain Old CLR Objects (POCOs). The only strict requirement for a persistent class is a no-argument constructor, which does not have to be public. (Proper behavior in some applications also requires special attention to the Equals() and GetHashCode() methods.)[1]

History

NHibernate was started by Tom Barrett, and later picked up by Mike Doerfler and Peter Smulovics. At the end of 2005, JBoss, Inc. (now part of Red Hat) hired Sergey Koshcheyev, the then lead developer of NHibernate, to work full-time on its future versions.[2] At the end of 2006 JBoss stopped the support to this project; it is now entirely developed and led by the community.

Version 1.0 mirrored the feature set of Hibernate 2.1, as well as a number of features from Hibernate 3.

NHibernate 1.2.1, released in November 2007, introduced many more features from Hibernate 3 and support for .NET 2.0, stored procedures, generics, and nullable types.

NHibernate 2.0

NHibernate 2.0 was released on August 23, 2008. It is comparable to Hibernate 3.2 in terms of features. With the version 2.0 release, NHibernate dropped support for .NET 1.1.[3]

NHibernate 2.1 was released July 17, 2009.

NHibernate 3.0

NHibernate 3.0 was released on December 4, 2010 and is the first version to use .NET 3.5, with features such as:

NHibernate 3.2

NHibernate 3.2 was released in April, 2011. Some of the new features were:[4]

NHibernate 3.3

NHibernate 3.3.0 was released on April 21, 2012.[5]

NHibernate 3.3.1 was released on June 12, 2012. [6]

NHibernate 3.3.2 was released on October 22, 2012. [7]

NHibernate 3.3.3 was released on March 17, 2013. [8]

NHibernate 4.0

NHibernate 4.0 was released on August 17, 2014. [9]

Contributions

As open source software, NHibernate has received many contributions from its users. Implementation of LINQ has allowed Language Integrated Query use with NHibernate.[10]

Sample

Here a code snippet to add an object to the database and shows how to retrieve, modify and update an object in the database using NHibernate.

//Add a Customer to the datastore

//'sessionFactory' is a thread-safe object built once per application lifetime (can take seconds to build)
//based on configuration files which control how database tables are mapped to C# objects
//(e.g. which property maps to which column in a database table)
//
//'session' is not thread safe and fast to obtain and can be thought of as a connection to the database
using (var session = sessionFactory.OpenSession()) 
{
    //transaction represents a db transaction
    using (ITransaction transaction = session.BeginTransaction()) 
    {
        //The line below adds the customer to NHibernate's list of objects to insert to the database
        //but it doesn't execute SQL insert command at this stage*.
        //*if the Id field is generated by the database (e.g. an auto-incremented number) 
        //then NHibernate will execute SQL INSERT when .Save is called  
        session.Save(new Customer { Id = Guid.NewGuid(), FirstName = "Boss", Age = 50 });

        //The call below will execute the SQL INSERT and commit the transaction
        transaction.Commit();
    }
}

//Retrieve the Customer from the database, modify the record and update the database
using (var session = sessionFactory.OpenSession())
{
    using (ITransaction transaction = session.BeginTransaction()) 
    {
        //session's Query returns IQueryable<Customer>.
        //Only when .FirstOrDefault is called will NHibernate execute the SQL query  
        Customer customer = session.Query<Customer>().Where(c => c.Token == token ).FirstOrDefault();
    
        //Now the customer is 'part of' the 'session' object and NHibernate keeps track of changes
        //made to it 
        if( customer != null ) 
        {
            //Changing a property of an object does NOT cause SQL to be executed
            customer.TokenVerified = true;
    
           //Committing the transaction results in an SQL UPDATE statement
           //NHibernate kept track of the fact that 'customer' has been changed since loading 
           transaction.Commit();
        }
    }
}

NHibernate's configuration may affect when NHibernate executes SQL statements.

See also

References

Bibliography

Microsoft MVC4 Nhibenate

This article is issued from Wikipedia - version of the 3/23/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.