Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Using CUBRID and NHibernate

An introduction web tutorial

June 5, 2013



In this tutorial we will show you how to use NHibernate with CUBRID in a simple demo web application that performs all the standard CRUD operations.

This tutorial was inspired by the following article, published on

NHibernate and MySQL - A simple example (credits goes to Johan Lundqvist, for publishing this article) 

What we intend to show here is that using CUBRID you can achieve with no effort the same results as in using MySQL with NHibernate!So we followed the same steps as in the original article, but using CUBRID as the RDBMS backend.




Required software/setup:


Setting up the project

In this tutorial you will learn how to create a simple web project that works with CUBRID and NHibernate to access a database table. 

Let's start by creating the project folder structure:

  • Open MS Visual Studio and create a blank ASP.NET website
  • Create a new Webform called Default.aspx and set it as the Start Page

Next, add to your project a reference to NHibernate.dll and to CUBRIDData.dll

After you have performed these operations your project tree should look like this:


The last thing we will need is a table in our available database (let’s use demodb). To keep things simple, we will create a table named site with just two columns:

  • An id column that will serve as primary key
  • A name column. The SQL is:
"id" integer AUTO_INCREMENT(1,1) NOT NULL,
"name" character varying(100) NOT NULL,


Configuring NHibernate

According to the documentation, you can configure NHibernate in three ways:

  • through the web.config
  • through code
  • using a separate configuration file just for NHibernate called hibernate.cfg.xml 

In this tutorial we will use the last approach as it seemed nice to have this separated from the other configuration stuff, so let's get going and take a look at the configuration file.

In your project, create a new xml file called hibernate.cfg.xml. To tell NHibernate that this is its configuration file, set the Build Action property to Embedded Resource and the Copy to Output Directory property to Copy always. Thehibernate.cfg.xml file consists of a few data that is quite simple to understand. The file with looks like this: 

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<!-- an ISessionFactory instance -->
<!-- properties -->
<property name="connection.provider">
<property name="connection.driver_class">
<property name="connection.connection_string">
<property name="dialect">
<!-- mapping files -->
<mapping resource="NHibernateCUBRID.Site.hbm.xml" assembly="NHibernateCUBRID" />
  • connection.provider sets which connection provider should be used by NHibernate to connect to the CUBRID database.
  • connection.driver_class sets which driver should be used and in this case, when using CUBRID, CUBRIDDriver is our choice
  • connection.connection_string is the connection string to the CUBRID database.
  • dialect states the NHibernate class name that enables certain platform dependent features, and in this case, since we are using CUBRID, the choice is CUBRIDDialect

The mapping part of the configuration file tells NHibernate which mapping files to use for the object/relational mapping, and we will describe that later in this tutorial.

The Persistent Class


NHibernate can use ordinary classes when persisting objects in a relational database, and uses a mapping technique to connect the persistent class properties to the columns in the relational database tables. 

For our simple tutorial, we will create a simple C# class called Site, and the code for that class is as follows:

namespace NHibernateCUBRID
public class Site
    private int id;
    public virtual int Id
        get { return id; }
        set { id = value; }
    private string name;
    public virtual string Name
        get { return name; }
        set { name = value; }
    public Site()

Site.cs contains two properties that correspond to the columns in a database table. NHibernate isn't really restricted to use only property types, all .NET types and primitives can be mapped, but in this simple example, we will only use int and string

The ID property is quite important since it will correlate to the primary key in the table; even if it is not mandatory to use this for NHibernate (which can handle identifiers internally), it is a normal architectural approach. 

As you can see from above, all the class public properties are declared virtual and this is because NHibernate utilizes this for some runtime enhancements which otherwise won't work according to the documentation, and it is also recommended that you provide a default constructor for the class.


The Mapping

To be able to use the Site class, it is necessary to create an NHibernate mapping file that contains the metadata that NHibernate uses for the object/relational mapping, i.e., connecting the class declaration, the properties to columns, and keys in the database tables.

So, we will create a new xml file called Site.hbm.xml and set its Build Action property to Embedded Resource.

The content of the file is:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateCUBRID.Site, NHibernateCUBRID" table="site">
<id name="Id" column="ID" type="int">
<generator class="native"></generator>
<property name="Name" column="name" type="String"></property>


The code

Now that we are finished with configuring NHibernate and implementing our object/relational model, we are ready to implement a simple webpage that performs all the CRUD operations on our database table by using object/relational mapping.

First thing we need to do is add a private ISessionFactory member to our Default class which will compile our mappings and create the NHibernate configuration that we need. ISessionFactory is, according to the NHibernate documentation, a thread safe cache of compiled mappings for a single database, and in the case when we have set the mapping files to be Embedded Resources, this is the only code that is needed to obtain such a factory:

private ISessionFactory factory =
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();



In this section we will see how can we retrieve the sites from our database table and dynamically populate an HTML table. So we will add an empty asp table to the webpage. Because we need to refresh this table every time an operation is performed, we will create a private method in our class that performs the query on the database and populates the table:

private void RefreshSitesTabe()
    using (ISession session = factory.OpenSession())
        ICriteriasc = session.CreateCriteria(typeof(Site));
        siteList = sc.List();
    for (int i = 0; i <siteList.Count; i++)
        TableRow row = new TableRow();
        TableCell selectCell = new TableCell();
        Button siteSelectButton = new Button();
        siteSelectButton.Text = "Select";
        siteSelectButton.ID = (i+1).ToString();
        siteSelectButton.Click += SelectButton_Clik;
        TableCell idCell = new TableCell();
        idCell.Text = ((Site)siteListi).Id.ToString();
        TableCell nameCell = new TableCell();
        nameCell.Text = ((Site)siteListi).Name;

The code uses an ISession instance which is described as a short lived object representing a conversation between the application and the persistent store, i.e., the database, and really wraps an ADO.NET connection. To get an instance of an ISession, you ask the ISessionFactory instance to open and return such an object through the function OpenSession(). 

One more thing: For each site we create one more column in the asp table that contains a button called Select. We will use this button to update and delete sites from the database. 




To insert a new site in the table, we need just a TextBox control and a Save button, so let’s add them to the default.aspx webpage. The important piece of code is in the save button click event handler: 

protected void SaveButton_Click(object sender, EventArgs e)
    Site s = newSite();
    using (ISession session = factory.OpenSession())
        s.Name = NewSiteTextBox.Text;

Again, we will use an instance of ISession which we obtain by calling the OpenSession() method form our ISessionFactory. After that, all we have to do is create a new Site object, set its Name property using the TextBox as input, use our session object to save our Site in the database, and refresh the HTML table to include the newly created site: 




To update a site in the database, we will use a TextBox control that is populated when the user clicks a select button from the sites table, and an Update button. The user then modifies the content of this textbox and clicks the update button. The update button event handler code is as follows: 

protected void UpdateButton_Click(object sender, EventArgs e)
    using (ISession session = factory.OpenSession())
        Site s = (Site)session.Load(typeof(Site), int.Parse(SelectedSiteTextBox.ToolTip));
        s.Name = SelectedSiteTextBox.Text;

Without going into too much implementation details, we issue the SaveOrUpdate command which instructs NHibernate that the info has been updated. To persist the changes to the database, it is essential to use the Flush command, otherwise the changes won't be propagated back to the database. After the update has been performed, we call the RefreshSitesTable() method to view the updated information. 


This operation is done in the same way as the UPDATE operation. The Delete button click event handler code is: 

protected void DeleteButton_Click(object sender, EventArgs e)
    using (ISession session = factory.OpenSession())
        Site s = (Site)session.Load(typeof(Site), int.Parse(SelectedSiteTextBox.ToolTip));

Note that a client confirmation dialog is displayed – we implemented this in the Page_Load event: 

 if (!Page.IsPostBack)


  this.Button2.Attributes.Add("onclick""return confirm('Are you sure you want to delete this site?');");





And this covers the things we wanted to show you in this tutorial! 

For more detailed information, please take a look at the project code.

See also… 



CUBRID ADO.NET Driver Wiki Page

CUBRID ADO.NET Driver download from

CUBRID NHibernate source code

CUBRID NHibernate Wiki page


We hope you enjoyed this first CUBRID NHibernate tutorial!

Please let us know your feedback and suggestions, so we can improve!


Thank you!

The CUBRID API team 

comments powered by Disqus
Page info
viewed 2730 times
translations en
posted 4 years ago by
updated 4 years ago by
View revisions
Share this article