NHibernate Framework - 1.0

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 C#
Fluent NHibernate Framework 2.0 to 3.0 C#

Compatibility

Core release Operating System Supported
v3/8.4.x Microsoft Windows / Linux
v2/8.3.x Microsoft Windows

Function Point, Quality and Sizing support

This extension provides the following support:

  • Function Points (transactions): a green tick indicates that OMG Function Point counting and Transaction Risk Index are supported
  • Quality and Sizing: a green tick indicates that CAST can measure size and that a minimum set of Quality Rules exist
Function Points (transactions) Quality and Sizing

Download and installation instructions

For .NET applications using NHibernate, the extension will be automatically installed. This is in place since October 2023. For upgrades, if the Extension Strategy is not set to Auto Update, you can manually install the extension.

What results can you expect?

Objects

Icon Type Description When 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 Operation Created and used when CRUD operation is performed on NHibernate Entity
NHibernate SQL Query Created for each SQL query found and resolved in an NHibernate method call
NHibernate HQL Query Created for each HQL query found and resolved in an NHibernate method call
NHibernate Unknown SQL Query Created when the query could not be resolved in an NHibernate method call
NHibernate Unknown Entity Created and used when the entity could not be resolved
NHibernate Unknown Entity Operation Used when CRUD operation is performed and Entity could not be resolved
Link Type Caller type Callee type NHibernate API's Supported

callLink 


C# Method

NHibernate Entity Operation


    ISession APIs
  • 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
callLink C# Method

NHibernate SQL Query

/ NHibernate HQL Query

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

NHibernate SQL Query

/ NHibernate HQL Query

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

NHibernate SQL Query

/ NHibernate HQL Query

Procedure
useLink NHibernate Entity Operation Table, View Created by WBS when DDL source files are analyzed by SQL Analyzer
callLink NHibernate Entity Operation Procedure
useLink

NHibernate SQL Query

/ NHibernate HQL Query

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

NHibernate SQL Query

/ 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);
            }
        }