• Javascript
  • Python
  • Go

How to Insert or Update Records using NHibernate

NHibernate is a powerful object-relational mapping (ORM) tool that allows developers to map and manage database records using object-oriente...

NHibernate is a powerful object-relational mapping (ORM) tool that allows developers to map and manage database records using object-oriented programming languages such as C# and Java. One of the key features of NHibernate is the ability to insert or update records in a database. In this article, we will explore the different methods and techniques for inserting or updating records using NHibernate.

Before we dive into the specifics, let's first understand the basic concepts of NHibernate. NHibernate uses a session object to interact with the database. This session object acts as a bridge between the application and the database, allowing us to perform various database operations such as querying, inserting, updating, and deleting records.

Inserting Records using NHibernate:

To insert a record into the database using NHibernate, we first need to create an instance of the entity we want to insert. For example, if we have a Customer class with properties like Name, Email, and Address, we can create a new instance of this class and set its properties accordingly.

Customer customer = new Customer();

customer.Name = "John Smith";

customer.Email = "john@example.com";

customer.Address = "123 Main Street";

Next, we need to open a new session using the SessionFactory object. This session will be responsible for persisting the changes to the database.

ISession session = SessionFactory.OpenSession();

Once we have the session open, we can use the Save() method to insert the record into the database.

session.Save(customer);

This will add a new record to the database table corresponding to the Customer entity. NHibernate will automatically generate the necessary SQL insert statement and execute it against the database.

Updating Records using NHibernate:

Updating records using NHibernate follows a similar approach as inserting records. We first need to retrieve the record we want to update from the database.

Customer customer = session.Get<Customer>(1); //assuming the record with id=1 exists in the database

Once we have the record, we can make changes to its properties and then use the Update() method to persist the changes to the database.

customer.Name = "Jane Smith";

customer.Email = "jane@example.com";

customer.Address = "456 Maple Street";

session.Update(customer);

This will generate the necessary SQL update statement and execute it against the database, updating the corresponding record.

Batch Updates using NHibernate:

NHibernate also provides the option to perform batch updates, where we can update multiple records at once instead of making individual database calls for each record. This can significantly improve performance when dealing with a large number of records.

To perform a batch update, we first need to create a query using the IQuery interface and specify the SQL update statement we want to execute.

IQuery query = session.CreateQuery("update Customer set Name = :name, Email = :email, Address = :address where Id = :id");

query.SetParameter("name", "Jane Smith");

query.SetParameter("email", "jane@example.com");

query.SetParameter("address", "456 Maple Street");

query.SetParameter("id", 1);

Next, we can use the ExecuteUpdate() method to execute the batch update.

query.ExecuteUpdate();

This will update all the records in the Customer table with the specified values, where the Id is equal to 1.

In conclusion, NHibernate provides a simple and efficient way to insert or update records in a database. By leveraging the power of object-oriented programming, NHibernate makes it easier for developers to manage database operations

Related Articles