NHibernate

NHibernate is a port of the Hibernate object–relational mapping (ORM) framework for the Microsoft .NET platform.

Uses your system voice; voice and rate in settings.

Snapshot

≈ 2 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

NHibernate is a port of the Hibernate object–relational mapping (ORM) framework for the Microsoft .NET platform. It provides a framework for mapping an object-oriented domain model to a traditional relational database. It is used to map an object-oriented domain model to a relational database, aiming to reduce the amount of manual coding required for data persistence.

NHibernate is free and open-source software, distributed under the GNU Lesser General Public License. //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 . // Only when .FirstOrDefault is called will NHibernate execute the SQL query Customer customer = session.Query ().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

List of object–relational mapping software .NET Persistence API (NPA)

Text derived from https://en.wikipedia.org/w/index.php?oldid=1305077434 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

11 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.