Rexiology::Past

Archive of original http://blog.rex.la...

Community

News

  • From Taiwan, living and working at Tokyo, Japan.

Recent Posts

Tags

Microsoft

跟我有關的

I.T. Related

Bloggers

線上廣播 (Online Radio)

.推薦閱讀

.推薦音樂

SharePoint Blogroll

Business

TechNet Related

日本相關

音樂 (Music)

Archives

Site Info



Locations of visitors to this page




Logos & Chicklets


GeoURL


Rex's Certifications
Rex's Certifications


Creative Commons授權條款
本 著作 係採用
Creative Commons 授權條款



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:  , , , ,

 

Comments

TrackBack said:

# November 16, 2005 1:28 PM

James said:

Thanks a lot - this really helped in a recent project!
# December 21, 2005 9:15 PM

UchihaJax said:

Whooo thankee. :)

That would have taken a while to find out myself, I found the same problem when I specified a property to be:

type=String(5000)
Which gave me this exception

"Nhibernate only allows string sizes up to 4000".

But using StringClob fixed it all.
You rule! :)
# December 22, 2005 2:52 AM

OJoE said:

Thanks, you saved my life... :)
# October 13, 2006 8:52 AM

E-one said:

Nevertheless this post is so old, THANKS!!!

You made my life easy :)

# July 17, 2008 4:33 AM