Lorenz Cuno Klopfenstein

Posts tagged "NHibernate"

This is an update over an old post about running Lucene.Net on a medium trust web hosting service with NHibernate.Search.

Lucene logo

After more than one year, the full-text search library has changed a bit: most notably, Lucene.Net has exited the "incubation" stage and is now an officially featured project of the Apache foundation. That's great news. Unfortunately, the project's homepage hasn't been updated and that caused a bit of confusion on my part. The updated SVN repositories are now here:

https://svn.apache.org/repos/asf/lucene/lucene.net/

and the stable binary packages are way outdated. I suggest you build your Lucene.Net dll from source code.

The NHibernate.Search source code has changed as well and includes several improvements. For instance, the issue I was reporting in my original post has disappeared (I'm not sure if this depends on a fix in the library or in my code, but the deadlock is gone) and the Optimize() method actually performs the optimization of Lucene's index files.

More...

Posted on Wednesday, January 20, 2010
176 Views
1 comments posted

I recently updated the source code to my Babil project in order to port it to the new ASP.NET MVC 2 beta. I decided I could as well update all other libraries the project depends on, which have been updated too in the meantime.

This took some time, but if you want to create a new website project based on ASP.NET MVC and NHibernate, here's the latest bits:

  • Get the ASP.NET MVC 2 beta source code from Codeplex and compile it. Remember to also compile the Microsoft.Web.Mvc.dll library that contains some useful code.
  • Get NHibernate 2.1, the latest release of the popular O/RM library.
  • Get the latest MySQL .NET connector (6.1.3) if you use NHibernate against a MySQL database.
  • If you use NHibernate.Search for full text indexing:
  • If you use NHibernate.Validator, get the source code and compile that too against the latest NHibernate DLL.
  • For your dependency injection needs, you might use Autofac. In that case, you also need to download the latest stable Autofac library and the source code of the web integration library. This integration library must be compiled using the beta version of ASP.NET MVC 2.

That's it, now throw everything in the /Bin folder and - crossing fingers - you should be able to finally write some own code.  :)

Posted on Wednesday, December 16, 2009
204 Views
0 comments posted

UTF I already posted about the problems I ran into using UTF-8 encoding on MySQL and PHP. Unfortunately, MySQL still seems to not be completely happy about Unicode characters, even if you work with ASP.NET and NHibernate (which both have complete Unicode support by default).

Therefore, in order to get your UTF strings stored in the database and get them back correctly in .NET, you'll first have to specify the charset you're using for the database connection in your connection string. For instance:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
	<reflection-optimizer use="false"/>
	<session-factory>
		<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
		<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
		<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
		<property name="connection.connection_string">
		Server=localhost;Database=db_name;User ID=user_id;Password=pwd;charset=utf8
		</property>
		<!-- other properties -->
	</session-factory>
</hibernate-configuration>

As far as I know, this should be equal to sending the "SET NAMES utf8;" command I used to send in PHP.

Then, you must ensure that your database tables actually use the Unicode encoding to store strings. This is especially true if your table creation SQL is automatically generated by NHibernate, through the GenerateSchemaCreationScript() method, because NHibernate will just use the default encoding (not Unicode). On Microsoft SQL Server it will instead use the Unicode column types like "nvarchar" and "ntext", preventing any problem.

Hence, when you create a new database, explicitly define the charset to use or MySQL will default to latin1:

CREATE DATABASE db_name
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_unicode_ci;

Then you can normally create all your tables and start using them. Test the database using some uncommon characters, like the "famous" dotless turkish ı, and try to do some upper/lowercase conversion on the database.

Posted on Tuesday, May 12, 2009
240 Views
0 comments posted

Disclaimer: this is a roundup of things I got from the NHibernate documentation, the ufficial forum or some random blog post, they seem to work for me but I'm not completely sure this is the "correct" way of doing things.

NHibernate logo The custom CMS I'm currently working on (and that is managing this website) relies on NHibernate as its O/RM, which currently provides two separate caching mechanisms.

The first one is the first level cache. It's a local cache to each separate NHibernate Session (a single ADO connection to the database, that usually is opened when the server receives an HTTP request and is closed again when the request is completed). NHibernate will keep all objects associated to that particular session in its cache. This cache is lost as soon as the session is disposed.

NHibernate also allows you to select one of the many cache providers you can find on NHForge.org as a second level cache. This type of cache is persistent, lives across multiple request and is used by all sessions concurrently. Roughly speaking, when an object instance is fetched from the database all values of the object are stored in the cache. When the same object is requested again, NHibernate will dehydrate the object using the values found in the cache which are associated to that particular identifier of the object.

This means that no instances are stored in the cache, but only values. As explained in the documentation, this means that if you manipulate objects loaded through NHibernate you won't risk disrupting the cache, while relationships and associations between instances will always be consistent.

More...

Posted on Monday, January 05, 2009
6971 Views
15 comments posted

I'm currently trying to use NHibernate.Spatial with SQL Server 2008 (which natively supports Geometry and Geography types and indices) and so far everything works (thanks to the really good wikis on NHibernate Forge).

Unfortunately the MsSql2008 dialect implemented in NHibernate.Spatial has a known bug: in case you decide to store a NULL value in a Geometry column, the SQL generated by NHibernate uses a completely wrong parameter type (using tinyint instead of the intended geometry).

To solve the problem you need to download the NHibernate.Spatial code via SourceForge and open up the \src\NHibernate.Spatial.MsSql2008\Type\MsSql2008GeometryType.cs file. At line 58 or so you'll find:

/// <summary>
/// Converts from GeoAPI geometry type to database geometry type.
/// </summary>
/// <param name="value">The GeoAPI geometry value.</param>
/// <returns></returns>
protected override SqlGeometry FromGeometry(object value)
{
    IGeometry geometry = value as IGeometry;
    if (geometry == null)
    {
        return null;
    }
    //...

As you can see, when the value you are trying to assign to a Geometry column is null, the MsSql2008GeometryType simply passes null to the component that actually writes data to the SQL query. You need to correct that line passing a valid NULL value back:

protected override SqlGeometry FromGeometry(object value)
{
    IGeometry geometry = value as IGeometry;
    if (geometry == null)
    {
        return SqlGeometry.Null;
    }
    //...

Compile everything and use the newly generated NHibernate.Spatial.MsSql2008.dll library to store NULL values correctly.  ;)

Posted on Thursday, December 11, 2008
Tagged as
507 Views
1 comments posted

An updated post about Lucene.Net and NHibernate.Search on medium trust has been published. (more...)

Posted on Sunday, October 26, 2008
3286 Views
12 comments posted

Following my previous article about how I deployed this new ASP.NET MVC website on IIS 6 and .NET 2.0, now I'll explain how I made NHibernate (an Object-Relational mapper for .NET) work on the same platform (while in the next article I'll tackle Lucene.NET and NHibernate.Search). (more...)

Posted on Saturday, October 25, 2008
1719 Views
18 comments posted
Back to Klopfenstein.net
Clemens Klopfenstein Serena Kiefer Lukas Tiberio Klopfenstein Lorenz Cuno Klopfenstein
English German