Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Using NMG (NHibernate Mappings Generator) with CUBRID


June, 2013

 

In this tutorial we will show you how to usethe NMG(NHibernate Mapping Generator) application with CUBRID, to generate the NHibernate database mappings & code.

 

Introduction

NMG - "NHibernate Mapping Generator" is an application that can generate  NHibernate mapping files and the corresponding domain classes from database tables:

  • It supports Oracle, SQLServer, PostgreSQL, MySQL, SQLite, Sybase, Ingres, and CUBRID
  • Can generate hbm.xml, Fluent NHibernate and NH 3.3 Fluent style of mapping files.
  • Has lots of preferences to control the property naming conventions.
  • Can generate Domain Entity and WCF Data Contracts too.
  • Can generate one table at a time or script entire DB in one go. (It can generate mapping for around 800 tables in under 3 minutes on my moderately powered laptop)
  • Supports ActiveRecord code generation. 
  • It is free. No licensing restrictions.
  • Has the option to generate NHibernate or MS validators

Most important for the CUBRID community is that NMG does support CUBRID starting with version 4.0:

 

NMG.jpg

 

Setup

You can download the latest version of NHibernate Mapping Generator, complete with CUBRID support here.

To get started with NMG just download it and unzip the file into a location of you convenience.

Then, run the NHibernateMappingGenerator.exe executable file.

In order to generate NHibernate mapping files with NMG we first have to connect to a CUBRID database. To do that, launch NMG and create a new connection using the New connection button. In the connection dialog, type a name of your choice for this connection, set the Database Type to CUBRID, edit the connection parameters accordingly to your database setup (because the connection is done using the CUBRID ADO.NET driver, the connection string must use this syntax; you can easily generate the connection string here) and click Save.

In this tutorial we will use the default CUBRID ADO.NET connection parameters for the "demodb" database.

 

Connect1.jpg

After creating the new connection click Connect and NMG will retrieve the list of the tables from the database and display the structure of the currently selected table:

Connect2.jpg

 

NHibernate mappings

As mentioned before, NMG can generate:

  • (“standard”) .hbm.xml files
  • By code NHibernate mappings
  • NHibernate 3.3 Fluent style of mapping files

Also, NMG generates the domain class code associated with the mapping files. These mapping styles can be configured from the Preferences tab:

 MappingStyles.jpg

You can find more information by examining the tool source code here.

Let's see some simple examples of mapping files that NMG can generate. We will use the Stadium table from the demodb database.

 

*.hbm.xml mapping

HBM.jpg

 

Fluent mapping

Fluent.jpg

 

By Code mapping

 ByCode.jpg

In addition, NMG also generates the corresponding Domain Class code:

Domain.jpg

As you can see, the NMG tool can significantly help the NHibernate CUBRID developer, by automatically generating all the mappings files and the domain classes’ code.

NMG is easy to use and it provides good support for CUBRID!

 

Examples

In this section we will see two examples of how NMG generates mapping files and domain classes.

The first example is a mapping of a 1:N relationship, and the second will be an example of how NMG maps a CUBRID CLOB type column.

 

1:N example

In this first example, we will show the generation of the mapping for a 1:N tables relationship.

For the purpose of this example we will create two simple tables that implement the 1:N relationship between a book and its chapters. The SQL used for the creation of the tables is:

CREATE TABLE "book"(
"id" integer AUTO_INCREMENT(1,1) NOT NULL,
"title" character varying(255) NOT NULL,
"author" character varying(255) NOT NULL,
CONSTRAINT pk_books_id PRIMARY KEY("id")
);

CREATE TABLE "chapter"(
"id" integer AUTO_INCREMENT(1,1) NOT NULL,
"title" character varying(100),
"bookid" integer,
CONSTRAINT  "fk_chapters_bookid" FOREIGN KEY ("bookid") REFERENCES "book"("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT pk_chapters_id PRIMARY KEY("id")
);

After creating the tables and you connect to the database, when you select these tables and generate the NHibernate code you will obtain:

Book.jpg

Chapter.jpg

The generated Domain Code for the two tables is:

namespace NMGCubrid {
    public class Book {
        private System.Nullable<int> id;
        private string title;
        private string author;
        public Book() {
            Chapters = new List<Chapter>();
        }
        public virtual IList<Chapter> Chapters { get; set; }
    }
}

 

namespace NMGCubrid {
    public class Chapter {
        private System.Nullable<int> id;
        private Book Bookid;
        private string title;
    }
}

The 1:N relationship is implemented in the Book class by the Chapters member who represents a list of the book's corresponding chapters, and in the Chapter class by the Bookid member who will hold the chapter's corresponding book. The relationship is mapped in the hbm.xml generated files in the same way:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="NMGCubrid" namespace="NMGCubrid" xmlns="urn:nhibernate-mapping-2.2">
<class name="Book" table="book" lazy="true" >
<id name="Id">
<generator class="identity" />
<column name="id" sql-type="INT" not-null="true" unique="true" />
</id>
<property name="Title">
<column name="title" sql-type="STRING" not-null="true" />
</property>
<property name="Author">
<column name="author" sql-type="STRING" not-null="true" />
</property>
<bag name="Chapters" inverse="true" cascade="none">
<key column="bookid" />
<one-to-many class="Chapter" />
</bag>
</class>
</hibernate-mapping>

 

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="NMGCubrid" namespace="NMGCubrid" xmlns="urn:nhibernate-mapping-2.2">
<class name="Chapter" table="chapter" lazy="true" >
<id name="Id">
<generator class="identity" />
<column name="id" sql-type="INT" not-null="true" unique="true" />
</id>
<property name="Title">
<column name="title" sql-type="STRING" not-null="false" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="Bookid">
<column name="bookid" sql-type="INT" not-null="false" />
</many-to-one>
<property name="Bookid">
<column name="bookid" sql-type="INT" not-null="false" />
</property>
</class>
</hibernate-mapping>

In the Book mapping file the relationship is mapped as bag element that has two child elements: a key element which specifies the column in the foreign key table that references the Book table, and a one-to-many element which specifies the name of the class that references Book.

In the Chapter mapping file, the relationship is represented by the many-to-one element which makes the mapping between the bookid column and the corresponding book.


A CUBRID CLOB example

To exemplify how NMG maps CUBRID CLOB objects, we will create a table that contains a CLOB column and use NMG on this table. The SQL used for creating the tables is: 

CREATE TABLE "clob_table"(
"id" integer NOT NULL,
"text" clob,
CONSTRAINT pk_clob_table_id PRIMARY KEY("id")
); 

Now let's see the table's structure definition in NMG:

 Clob.jpg

Notice that NMG detected the C# type of our CLOB column as CUBRID.Data.CUBIDClient.CUBRIDClob.

This information is also used in the generation of the Domain Code class:

namespace NMGCubrid {
    public class ClobTable {
        private System.Nullable<int> id;
        private CUBRID.Data.CUBRIDClient.CUBRIDClob text;
    }
}

 

As for the NHibernate mapping, NMG does NOT detect the NHibernate type for columns and does not include it in the xml mapping.

This is because the standard C# data type, like System.Int and System.String, are detected in NHibernate by reflection, but that is not the case for custom (=non-standard) data types like CUBRIDCLOB.

NMG will generateout-of-the-box the following xml mapping:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="NMGCubrid" namespace="NMGCubrid" xmlns="urn:nhibernate-mapping-2.2">
<class name="ClobTable" table="clob_table" lazy="true" >
<id name="Id">
<generator class="identity" />
<column name="id" sql-type="INT" not-null="true" unique="true" />
</id>
<property name="Text">
<column name="text" sql-type="CLOB" not-null="false" />
</property>
</class>
</hibernate-mapping>

 

So in order to use this mapping in a project, we MUST SET MANUALLY the NHibernate type by adding the type attribute to the property element corresponding to the CLOB column:

<property name="Text" type="CUBRIDClob">
<column name="text" sql-type="CLOB" not-null="false" />
</property>

 

Once we do this manual step we are good to go – we can start using the generated files to create CUBRID NHibernate applications!

For examples and tutorials related to creating such applications, please visit the CUBRID NHibernate home page.

 

References 

Resource

Address

NHibernate Mapping Generator

nmg.codeplex.com/

CUBRID NHibernate source code

http://svn.cubrid.org/cubridapis/NHibernate

CUBRID NHibernate Wiki page

http://www.cubrid.org/wiki_apis/entry/using-cubrid-and-nhibernate

We hope you enjoyed this 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 6770 times
translations en
Author
posted last year by
ovidiuguta
Contributors
updated last year by
View revisions
tagged
Share this article