Jump to content

NHibernate

From Wikipedia, the free encyclopedia
NHibernate
Stable release
5.5.0 / December 24, 2023; 10 months ago (2023-12-24)
Repository
Written inC#
Operating systemCross-platform
Platform.NET 4.6.1, .NET Standard 2.0, .NET Core 2.0, and Mono
TypeObject–relational mapping
LicenseGNU Lesser General Public License 2.1
Websitehttp://nhibernate.info

NHibernate is a port of the Hibernate object–relational mapping (ORM) tool 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 and open-source software that is distributed under the GNU Lesser General Public License.

Feature summary

[edit]

NHibernate's primary feature is mapping from .NET classes to database tables (and from CLR data types to SQL data types). NHibernate provides data query and retrieval tools. It generates the SQL commands and removes the need of 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

[edit]

Tom Barrett started NHibernate, 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. NHibernate is now entirely developed and led by the community. The NHibernate logo, designed by Andrew Mayorov, was chosen sometime before 2008. It features a dormouse, which is known for its long periods of hibernation.[3]

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

[edit]

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.[4]

NHibernate 2.1 was released July 17, 2009.

NHibernate 3.0

[edit]

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

  • Integrated LINQ support
  • Strongly typed criteria-like API called QueryOver
  • New AST-based parser for HQL engine
  • Support for lazy loading columns

NHibernate 3.2

[edit]

NHibernate 3.2 was released in April, 2011. New features included:[5]

  • Mapping by code: fluent configuration, .hbm.xml files are no longer required;
  • Subselect: ability to map SQL views as entities;
  • HQL paging: TAKE and SKIP on HQL;
  • Integrated bytecode provider: one less DLL to deploy.

NHibernate 4.0

[edit]

NHibernate 4.0 was released on August 17, 2014.[6] This version requires .NET Framework 4.0 or later.

NHibernate 5.0

[edit]

NHibernate 5.0 was released on October 10, 2017. It provides support for asynchronous programming.[7] This version requires .NET Framework 4.6.1 or later.

NHibernate 5.1

[edit]

NHibernate 5.1 was released on March 17, 2018. It supports .NET Standard 2.0 and .NET Core 2.0.[8]

NHibernate 5.2

[edit]

NHibernate 5.2 was released on December 4, 2018.[8]

NHibernate 5.3

[edit]

NHibernate 5.3 was released on July 19, 2020.[9]

Contributions

[edit]

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

NHibernate Profiler

[edit]

The NHibernate Profiler is an Object–Relational Mapping tool (ORM) that serves as a real-time visual debugger for NHibernate. It identifies inefficient SQL data queries to eliminate unnecessary work by the database to boost overall performance of the application. The NHibernate Profiler also alerts users to data queries that cost too much in time and directs them to the exact line in the C# code.[11]

Sample

[edit]

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

[edit]

References

[edit]
  1. ^ hibernate.org - Equals and HashCode
  2. ^ [Hibernate] NHibernate joins Hibernate at JBoss Inc
  3. ^ Berggren, Oskar (2012-10-20). "nhibernate-core/logo/Logo History.txt at master · nhibernate/nhibernate-core". GitHub. Retrieved 2024-04-24.
  4. ^ From NH1.2.1GA to NH2.0.0 - NHibernate
  5. ^ "NHibernate 3.2 Released". weblogs.asp.net. 8 August 2011. Retrieved 2019-01-25.
  6. ^ "NHibernate 4.0.0.GA released". groups.google.com. Retrieved 2019-01-25.
  7. ^ "NHibernate 5.0 Released - NHibernate". nhibernate.info. Retrieved 2019-01-25.
  8. ^ a b "NHibernate 5.2.0 Released - Google Groups". Retrieved 2019-01-25.
  9. ^ "NHibernate 5.3 Released". nhibernate.info. Retrieved 2020-09-10.
  10. ^ Implementing Linq for NHibernate: A How To Guide - Part 1
  11. ^ "NHibernate Profiler". hibernatingrhinos.com. Hibernating Rhinos. Retrieved 2020-05-20.

Bibliography

[edit]
[edit]