This documentation is not maintained. Please refer to doc.castsoftware.com/technologies to find the latest updates.

Extension Id

com.castsoftware.dotnet.nhibernate

What's new?

See NHibernate Framework - 1.0 - Release Notes for more information.

Description

This extension provides support for NHibernate Framework and Fluent NHibernate Framework

In what situation should you install this extension?

If your C# application uses the NHibernate/Fluent NHibernate Framework and you want to view these object types and their links, then you should install this extension.

Technology support

Component

Version

Supported

Supported Technology
NHibernate Framework

2.0 to 5.4

(tick)C#
Fluent NHibernate Framework

2.0 to 3.0

(tick)C#

AIP Core compatibility

This extension is compatible with:

AIP Core release

Supported

8.3.x(tick)

Supported DBMS servers

This extension is compatible with the following DBMS servers:

DBMSSupported
CAST Storage Service/PostgreSQL(tick)

Download and installation instructions

For .NET applications using NHibernate, the extension will be automatically installed by CAST Console. This is in place since October 2023.

For upgrade, if the Extension Strategy is not set to Auto update, you can manually install the extension using the Application - Extensions interface.

What results can you expect?

Once the analysis/snapshot generation is completed, you can view the results. The following objects and  links will be displayed in CAST Enlighten:

Objects

IconType DescriptionWhen is this object created?

NHibernate Entity Created when an entity is found in hbm xml file or ClassMap is being inherited in the Map class 

NHibernate Entity OperationCreated and used when CRUD operation is performed on NHibernate Entity

 

DotNet NHibernate SQL QueryCreated for each SQL query found and resolved in an NHibernate method call

 

DotNet NHibernate HQL Query

Created for each HQL query found and resolved in an NHibernate method call
Link TypeCaller typeCallee typeNHibernate API's Supported

callLink 


C# Method

NHibernate Entity Operation


  • NHibernate.ISession.Delete
  • NHibernate.ISession.DeleteAsync
  • NHibernate.ISession.Get
  • NHibernate.ISession.GetAsync
  • NHibernate.ISession.Load
  • NHibernate.ISession.LoadAsync
  • NHibernate.ISession.Update
  • NHibernate.ISession.UpdateAsync
  • NHibernate.ISession.Query
  • NHibernate.ISession.QueryOver
callLinkC# Method

DotNet NHibernate Sql Query

/ DotNet NHibernate HQL Query

  • NHibernate.ICriteria.List
  • NHibernate.ICriteria.ListAsync
  • NHibernate.ICriteria.UniqueResultAsync
  • NHibernate.ISession.GetNamedQuery
  • NHibernate.IQuery.ExecuteUpdate
  • NHibernate.AdoNet.MySqlClientSqlCommandSet.ExecuteNonQuery
  • NHibernate.Engine.IBatcher.ExecuteNonQuery
  • NHibernate.Engine.IBatcher.ExecuteNonQueryAsync
  • NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery
  • NHibernate.AdoNet.AbstractBatcher.ExecuteNonQueryAsync
  • NHibernate.Engine.IBatcher.ExecuteReader
  • NHibernate.Engine.IBatcher.ExecuteReaderAsync
  • NHibernate.AdoNet.AbstractBatcher.ExecuteReader
  • NHibernate.AdoNet.AbstractBatcher.ExecuteReaderAsync
useLinkDotNet NHibernate HQL QueryNHibernate Entity operation
  • NHibernate.ISession.GetNamedQuery
  • NHibernate.IQuery.ExecuteUpdate
  • NHibernate.AdoNet.MySqlClientSqlCommandSet.ExecuteNonQuery
  • NHibernate.Engine.IBatcher.ExecuteNonQuery
  • NHibernate.Engine.IBatcher.ExecuteNonQueryAsync
  • NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery
  • NHibernate.AdoNet.AbstractBatcher.ExecuteNonQueryAsync
  • NHibernate.Engine.IBatcher.ExecuteReader
  • NHibernate.Engine.IBatcher.ExecuteReaderAsync
  • NHibernate.AdoNet.AbstractBatcher.ExecuteReader
  • NHibernate.AdoNet.AbstractBatcher.ExecuteReaderAsync
useLink

DotNet NHibernate Sql Query

/ DotNet NHibernate HQL Query

Table, ViewCreated by SQL Analyzer when DDL source files are analyzed
callLink

DotNet NHibernate Sql Query

/ DotNet NHibernate HQL Query

Procedure
useLinkNHibernate Entity OperationTable, ViewCreated by WBS when DDL source files are analyzed by SQL Analyzer
callLinkNHibernate Entity OperationProcedure
useLink

NHibernate NHibernate Sql Query

/ DotNet NHibernate HQL Query

Missing TableCreated by Missing tables and procedures for .Net extension when the object is not analyzed.
callLink

NHibernate NHibernate Sql Query

/ DotNet NHibernate HQL Query

Missing Procedure

Code Examples 

Select Operation

Using hbm.xml file
  public static void studentGet(NHibernate.ISession session)
        {
            var stdnt = session.Get<Student>(1);
            Console.WriteLine("Retrieved by ID");
            Console.WriteLine("{0} \t{1} \t{2}", stdnt.ID,
               stdnt.FirstMidName, stdnt.LastName);
        }

Using Fluent Nhibernate mapping
using FluentNHibernate.Mapping;

namespace NHibernateExample
{
    class ParentMap : ClassMap<Parent>
    {
        public ParentMap()
        {
            Id(x => x.ID);
            Map(x => x.FirstMidName);
            Map(x => x.LastName);
            Table("ParentFluent");
        }
    }
}  

public static void parentUpdate(NHibernate.ISession session)
        {
            var parentGet = session.Get<Parent>(1);
            Console.WriteLine("Retrieved by ID");
            Console.WriteLine("{0} \t{1} \t{2}", parentGet.ID, parentGet.FirstMidName, parentGet.LastName);

            Console.WriteLine("Update the last name of ID = {0}", parentGet.ID);
            parentGet.LastName = "Donald";
            session.Update(parentGet);
            Console.WriteLine("\nFetch the complete list again\n");

        }

Update Operation

Update
public static void studentUpdate(NHibernate.ISession session, Student stdntGet)
        {
           
            session.Update(stdntGet);
            Console.WriteLine("\nFetch the complete list again\n");

        }

Delete Operation

Delete
public static void studentDelete(NHibernate.ISession session, Student stdntDel)
        {
            
            session.Delete(stdntDel);
            Console.WriteLine("\nFetch the complete list again\n");

        }

Query String

Execute Update
public static void deleteDataUsingQuery(ISession session)
        {
            IQuery query = session.CreateQuery("DELETE FROM Customer WHERE Id = '12345'");
            query.ExecuteUpdate();
        }

ExecuteNonQuery Using ADOSQLCommandSet
public static void updateDataUsingADOSQLCommandSetNonQuery(ISession session, CancellationToken cancellationToken)
        {
            MySqlClientSqlCommandSet mySqlClientSqlCommandSet = new MySqlClientSqlCommandSet(5);
            DbCommand dbCommand = session.Connection.CreateCommand();
            for (int i = 0; i < 5; i++)
            {
                dbCommand.CommandText = "INSERT INTO Customer (CustomerID, Name) VALUES (" +i.ToString()+ ", 'John Doe')";
                mySqlClientSqlCommandSet.Append(dbCommand);
            }
            
            mySqlClientSqlCommandSet.ExecuteNonQuery();

        }

ExecuteNonQuery Using EngineNonQuery
public static void updateDataUsingEngineNonQuery(ISession session, CancellationToken cancellationToken)
        {
            IBatcher batcher = (IBatcher)session.CreateQueryBatch();
            batcher.BatchSize = 5;
            DbCommand dbCommand = session.Connection.CreateCommand();
            dbCommand.CommandText = "INSERT INTO Customer (CustomerID, Name) VALUES (1, 'John Doe')";

            batcher.ExecuteNonQuery(dbCommand);
        }

ExecuteNonQuery Using ADO
public static void updateDataUsingADONonQuery(ISession session, CancellationToken cancellationToken)
        {
            DbCommand dbCommand = session.Connection.CreateCommand();
            dbCommand.CommandText = "INSERT INTO Customer (CustomerID, Name) VALUES (1, 'John Doe')";

            AbstractBatcher abstractBatcher = (AbstractBatcher)session.CreateQueryBatch();
            abstractBatcher.ExecuteNonQuery(dbCommand);
        }

ExecuteReader Using ADO
public static void updateDataUsingADOReader(ISession session, CancellationToken cancellationToken)
        {
            DbCommand dbCommand = session.Connection.CreateCommand();
            dbCommand.CommandText = "SELECT * FROM Parent p WHERE p.ID IN( SELECT ID FROM Teacher WHERE ID = '12345')";

            AbstractBatcher abstractBatcher = (AbstractBatcher)session.CreateQueryBatch();
            DbDataReader reader = abstractBatcher.ExecuteReader(dbCommand);
        }

ExecuteReader Using Engine
public static void updateDataUsingEngineReader(ISession session, CancellationToken cancellationToken)
        {
            IBatcher batcher = (IBatcher)session.CreateQueryBatch();
            batcher.BatchSize = 5;
            DbCommand dbCommand = session.Connection.CreateCommand();
            dbCommand.CommandText = "UPDATE Employee SET FirstMidName = 'Robert' WHERE ID = '12345'";

            DbDataReader reader = batcher.ExecuteReader(dbCommand);
        }

Criteria
public static void studentSelect(NHibernate.ISession session)
        {
            var cc = session.CreateCriteria<Student>();
            var students = cc.List<Student>();

            foreach (var student in students)
            {
                Console.WriteLine("{0} \t{1} \t{2}", student.ID,
                   student.FirstMidName, student.LastName);
            }
        }

Query and QueryOver
public static void fetchTouristUsingQuery(NHibernate.ISession session)
        {
            var tourists = session.Query<Tourist>().Where(c => c.FirstMidName.StartsWith("H"));

            foreach (var tourist in tourists.ToList())
            {
                Console.WriteLine(tourist);
            }
        }

public static void fetchVendorUsingQueryOver(NHibernate.ISession session)
        {
            var vendors = session.QueryOver<Vendor>().Where(x => x.LastName == "Laverne");

            foreach (var vendor in vendors.List())
            {
                Console.WriteLine(vendor);
            }
        }

Named Queries

Named Queries are generated with the help of hbm.xml files relating to the entity. Below is an example of one such hbm.xml file :

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
	<class name="Employee" table="employees">
		<id name="Id" column="id">
			<generator class="native" />
		</id>
		<property name="Name" column="name" />
		<property name="Age" column="age" />
		<property name="Salary" column="salary" />

		<sql-query name="GetEmployeesWithSalaryGreaterThan">
			SELECT *
			FROM employees
			where Salary > :minSalary
		</sql-query>

		<query name="GetEmployeesWithSalaryLessThan">
			SELECT *
			FROM Employee
			where :minSalary > Salary
		</query>
	</class>
</hibernate-mapping>
Named SQL Queries
Named SQL Queries
public static void fetchEmployeeUsingNamedSQLQuery(NHibernate.ISession session)
        {
            var query = session.GetNamedQuery("GetEmployeesWithSalaryGreaterThan");
            query.SetParameter("minSalary", 50000m);

            var results = query.List<Employee>();

            foreach (var employee in results)
            {
                Console.WriteLine("{0}: {1}", employee.Name, employee.Salary);
            }
        }

Named HQL Queries
Named HQL Queries
public static void fetchEmployeeUsingNamedQuery(NHibernate.ISession session)
        {
            var query = session.GetNamedQuery("GetEmployeesWithSalaryLessThan");
            query.SetParameter("minSalary", 50000m);

            var results = query.List<Employee>();

            foreach (var employee in results)
            {
                Console.WriteLine("{0}: {1}", employee.Name, employee.Salary);
            }
        }