Page tree
Skip to end of metadata
Go to start of metadata

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. More specifically the extension will identify:

  • "callLink" from C# methods to NHibernate Entity Operation objects.
  • "callLink" from C# methods to DotNet NHibernate SQL Query objects.
  • "callLink" from C# methods to DotNet NHibernate HQL Query objects and operation based "useLink" from DotNet NHibernate HQL Query objects to NHibernate Entity Operation objects.

Technology support

Component

Version

Supported

Supported Technology
NHibernate Framework

3.0.0 to 5.4.x

(tick)C#
Fluent NHibernate Framework

1.4.0 to 3.2.x

(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

The extension will not be automatically downloaded and installed in CAST Console. If you need to use it, should 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

IconDescription

NHibernate Entity

NHibernate Entity Operation

 

DotNet NHibernate SQL Query

 

DotNet NHibernate HQL Query

Link TypeSource and destination of linkNHibernate API's Supported

callLink 


Between the caller C# Method and NHibernate Entity object


  • 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
callLinkBetween the caller C# method and DotNet NHibernate Sql Query object/ DotNet NHibernate HQL Query object
  • NHibernate.ISession.CreateQuery
  • NHibernate.ISession.CreateCriteria
  • NHibernate.ISession.CreateSQLQuery
  • NHibernate.ISession.GetNamedQuery
useLinkOperation based "useLink" between DotNet NHibernate HQL Query object and NHibernate Entity operation object
  • NHibernate.ISession.GetNamedQuery
  • NHibernate.ISession.CreateQuery

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

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

 Create SQL Query
public static void updateDataUsingSQLQuery(ISession session)
        {
            IQuery query = session.CreateSQLQuery("UPDATE Teacher SET FirstMidName = 'Robert' WHERE ID = '12345'");
        }

 Create Criteria
public static void studentSelect(NHibernate.ISession session)
        {
            var students = session.CreateCriteria<Student>().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);
            }
        }

  • No labels