NHibernate With Blackfish

by John 1/17/2009 3:08:00 AM

I've just started to explore NHibernate. About the first thing I noticed was that it didn't support Blackfish. I had taken the opportunity to download the source code. It was while I was doing some debugging I noticed the classes used for individual database support. With a few hours of investigation I was able to get a couple of classes up and running with Blackfish.

I have uploaded a project to CodeCentral, it consists of a very simple example, a Blackfish database and a couple of csharp files.

If you download the example you should find three directories. 


1) BlackfishSupport - which contains the 2 C# files that you need to add to the NHibernate solution

2) database - A sample database. 

3) NHibernateConsoleApplication - A very small example I developed using Delphi Prism.

So having downloaded the NHibernate source the first thing you need to do is add the 2 C# files . Assuming the source was at the following locationS


BlackfishDialect.cs goes into H:\develop\NHibernate-2.0.1.GA\src\src\NHibernate\Dialect


BlackfishDriver.cs  goes into H:\develop\NHibernate-2.0.1.GA\src\src\NHibernate\Driver

Having done that you need to add those 2 files to the HHibernate project and rebuild.

If you take a look at the files you might notice there isn't exactly very much source, combined with the fact that I have only built one example using NHibernate I would classify my Blackfish support as "experimental" :)

Having compiled the solution you should now be able to open NHibernateConsoleApplication in Delphi Prism.

The first thing your going to have to do is modify the app.config to point to the right location for the Blackfish database file. I have 

      <property name="connection.connection_string">database=C:\Documents and Settings\JohnMoshakis\Application Data\nhibernate.jds;user=SYSDBA;password=masterkey;create=true;protocol=TCP;host=localhost</property>

which is probably different on your machine.

The example has 2 classes Customer and Company and their mapping files. The code is pretty simple it just adds a company and a customer and then performs a query to customers.

Anyway if you decide to download the source, I hope its useful. I intend to continue exploring NHibernate with Blackfish as my database. 

My example can be found here.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


NHibernate | Delphi Prism | Blackfish

Schema generation using database migrations - NorthWind for Blackfish

by John 7/20/2008 10:20:00 PM

Recently I've been maintaining my database schema changes using DBXExpress, more about the process I follow can be found here

I'm currently moving an application from MS Sql Server to Blackfish and unfortunately all I really have is a bunch of MS Sql specific scripts. I wished I had a set of migrations that I could use as the basis for my Blackfish database, so I decided to dive into DBExpress again and I have extended the MSBuild tasks I created to generate Delphi code that I could use as a starting point.

In the screenshot below can be seen the tables of the SQL Server version of the "NorthWind" database and on the right a Blackfish version.

An updated version of the original code can be found here.

As with the previous version having downloaded the code from codecentral, the first thing that needs to be done is to open up the "MigrationProjectGroup.groupproj" projectgroup

in the root directory. In the new version I have added a new package called Moshine.SchemaGenerationTest. In the download it contains a schema generation which are .pas files used to create the Northwind tables using dbxexpress. If you wish to re-generate, you can remove the .pas files from this package so that it would look like the screenshot below.



The important part of the project are the 2 msbuild target files and an app.config

The app.config contains two connection strings, one for MSSQL Server and the other for the Blackfish database that will be created. You are quite likely going to have to modify those so that they match your environment.

Generation.targets contains the specification for the schema generation and a copy can be seen below

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask TaskName="Moshine.Migration.MSBuild.TMigrationSchemaGenerationTask"
            AssemblyFile="..\Moshine.MSBuildMigration\bin\Moshine.MSBuildMigration.dll" />


    <Target Name="SchemaGeneration">
        <TMigrationSchemaGenerationTask  UpperCaseTableNames="$(UpperCaseTableNames)" Namespace="$(Namespace)" DestinationDirectory="$(DestinationDirectory)" MigrationAssembly="$(MigrationAssembly)" ConnectionName="$(ConnectionName)" />


DestinationDirectory contains the target directorty for the schema generation, your most likely going to have to change this.

Once the projectgroup has been built you should be able to move to a RAD Studio command prompt from which you can generate the schema.

The commandline from the Moshine.SchemaGenerateTest directory is MSBUILD generation.targets -t:SchemaGeneration 

You can also execute the MSBuild task from the IDE by right clicking on the  Generation.targets file in the project manager and performing a  targets/schemageneration

The actual code that performs the code generation is in the Moshine.Migration.Framework project as Moshine.Migration.Framework.MigrationGenerator.pas. I have tested against the MSSQL Server database Northwind that is used in quite a few Microsoft demos and against the database I'm migrating. In theory it should work against other MS SQL Server databases and any other database supported by DBXExpress. I haven't supported every datatype so you might get an error like this.

In which case you will have to modify the generator code, which shouldn't be too difficult :)

Assuming the schema generation went ok you should have .pas files in the  generation directory. Its 1 file per database table.

The screenshot below shows the files for Northwind

You can then add the files to the SchemaGenerationTest project and rebuild. The resulting assembly is going to be used to build the Blackfish database.

The generated code looks like the sample below


procedure TCreateTableShippersMigration.Up;
tableMetaData: TDBXMetaDataTable;
newTDBXWideVarCharColumn: TDBXWideVarCharColumn;
newTDBXInt32Column: TDBXInt32Column;
tableMetaData := TDBXMetaDataTable.Create();
newTDBXInt32Column:= TDBXInt32Column.Create('ShipperID');
if(not self.Provider.CheckColumnSupported(newTDBXInt32Column))then
newTDBXWideVarCharColumn:= TDBXWideVarCharColumn.Create('CompanyName',40);
newTDBXWideVarCharColumn:= TDBXWideVarCharColumn.Create('Phone',24);

This is DBXExpress Delphi code to create the SHIPPERS database table. Each class corresponds to 1 step in a migration. 

Having successfully built the assembly the second targets file Migration.Targets in the SchemaGenerationTest project can be used. You can right click on the

Migration.Targets file in the project manager and perform Targets/migration. 

From a CodeGear command prompt this would be MSBUILD migration.targets-t:migration. Assuming that the migration was performed you should have a Blackfish database

with the tables created. As I mentioned before you be able to perform a code generation and migration against any database that DBXExpress supports.

The generated code only creates the tables from the source schema but it should be a good starting point from which you can add your own code to further extend the migration and the database schema.

Feedback is welcome and the source can be found in CodeCentral here 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Database | migration | dbx4 | msbuild | Delphi | Blackfish

Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Name of author John Moshakis
I'm a software developer living in Toronto..

E-mail me Send mail


<<  June 2022  >>

View posts in large calendar


    Recent posts

    Recent comments



    Don't show


      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      © Copyright 2022

      Sign in