NHibernate Tips - If you are mapping to a ntext field on a SQL Server database...
Took me some time to find out the problem.
I was saving my serialized objects xml string to my database. SQL server nvarchar had a max length of 4000 chars, it's better to use a ntext to storing those xml string.
later found that the data saved to database was always been turncated thus caused the objects not able to be deserialized back. also found that the turncated size was always exactly 4000 chars. regardless that I've setting that field at table schema to be a ntext.
the cause of this was the NHibernate mapping file. if not specified the type in the mapping file, usually NHibernate used reflection to determine the type for you. it's also worked to a ntext field, although, if not specified, it will convert a .NET string type to nvarchar by default. that's why I always got only 4000 chars back.
To workaround this, after reading the reference chm file of NHibernate, there was a type named "StringClob" for CLOB like types, after declaratively specifying this type in that field in the mapping file, things go normal, now I got exactly the length of string saved to database back to my program for the preparation of object deserialization.
the reference chm file stated that it's only happened in SqlClient, Oracle should normally work without problems. later testing my program that switching to Oracle database, it was working without specifying the StringClob type in the mapping file provided for Oracle database mapping.
So if you are working on using NHibernate to connect your solution to a SQL database, and happened to had ntext fields around your table schema, be sure to specify StringClob type to prevent incorrect convertion from NHibernate. FYI.
Technorati Tags: hibernate , nhibernate , orm , database , programming