NuGet

NuGet is distributed as a Visual Studio extension. It supports multiple programming languages, including .NET Framework packages and Native packages written in C++. The application need some Frameworks to work installed from NuGet:

  • MySQL.Data by Oracle v6.10.8
  • MySQL.Data.Entity by Oracle v6.10.8
  • NHibernate by NHibernate community, Hibernate community

NHibernate Class

The class will represent the object. We have to create properties in the class that will match the fields in the database.

public virtual int property { get; set; } sets a class property that matches a field in the database. Note the property is defined with a virtual keyword. This makes it possible for the fields to be overridden.

NHibernate Mapping

NHibernate uses XML files for mapping database fields to objects properties. The XML file should always end with *.hbm.xml extension. In Solution Explorer of MyClass.hbm.xml the Build Action has to be set as Embedded Resource.

hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2” assembly=”CustomerRecords” namespace=”CustomerRecords” is the root element of the mapping file. The element has xmlns, assembly and namespace attributes. The assembly and namespace attributes should match your project assembly and namespace values. Assembly name and namespace name can be found in application’s Properties.

class name=”Customers” defines the class element.

id name=”customer_id” defines primary key. In our case its customer_id

property name=”field_name” matches the fields in the table that you wish to manipulate using NHibernate.

NHibernate Connection String MySQL

x.ConnectionString our connection string will connect to the local machine because we set the server property to a dot. (e.g. “Database=test_db;Data Source=localhost;Port=3306;User Id=admin;Password=123”)

x.Driver<MySqlDataDriver>(); we will work with MySqlDataDriver

x.Dialect<MySQL55Dialect>(); we will use the dialect for MySQL Server. This will make available the features in MySQL Server.

Executing Hibernate Query Language (HQL)

Hibernate Query Langauge (HQL) is very similar to SQL.

  • FROM… indicates this is a SELECT query in SQL language
  • WHERE… works the same as in SQL

In case of mapping

class name=”MyClass” table=”MY_TABLE”

The query should be

session.CreateQuery(“FROM MyClass”)

Transactions

CreateQuery() with the select clause picks which objects and properties to return in the query result set.

Save() takes a new object without an identifier and attaches it to the session. The object will be INSERT‘ed.

Update() takes an existing object that has an identifier but is not in the session and attaches it to the session. The object will be UPDATE‘d.

SaveOrUpdate() looks at the identifier and decides what is necessary in the above. The object will be INSERT‘ed or UPDATE‘d if already exists. The object will be UPDATE‘d.

Delete() will remove an object’s state from the database. The object will be DELETE‘d.

Commit() will flush the session and commit the transaction.

Flush() will force the SQL INSERT.

Close() represents the end of a session.