18

My DB schema has a string as a varchar(max). I have read the other questions concerning setting Length to more than 4000 or 8000 so that it really generates a (n)varchar(max) in the mapping but when I use Length(10000) in my mapping class, the hbm file actually shows length="10000" and if I save an entity with more than 10000 chars, it is actually truncated to exactly 10000 chars.

I don't want any truncation.

(using NH3-alpha2 and FNH trunk)

2 Answers 2

21

It would appear that this is an old problem which is now resurfacing in NHibernate 3.x builds; you can read about the workarounds here.

<property name="Blob" column="Blob" type="StringClob" />
<!-- this generates an nvarchar(max), data is truncated at the length specified: string(10000) -->
<property name="BlobLength" column="BlobLength" type="StringClob" length="10000" />
<!-- this mapping works! for generation, reading and writing -->
<property name="BlobSqlType" type="StringClob" >
    <column name="BlobSqlType" sql-type="nvarchar(max)" />
</property>

Calling CustomType("StringClob") should fix the problem.

Note: I have updated the original link I posted as it was outdated.

2
  • In an hbm mapping file, simply use type="StringClob" instead of type="string" and you're done. This definitely works for NH 3.3, but try it with for any NH version >= 3.0.
    – Oliver
    Commented Jun 13, 2012 at 15:58
  • Note that StringClob will generate a nvchar(255) on SQL Server 2008, and instead the workaround is to use a .Length(10000). This will create a nvchar(max) column type.
    – Astaar
    Commented Mar 29, 2013 at 14:20
17

This mapping should work:

<property name="TheProperty" type="StringClob">
  <column name="TheColumn" sql-type="nvarchar(max)" />
</property>

Just look for the fluent equivalent.

6
  • 2
    do you think this will ever be treated properly in NHib 3? Kind of sad to see the sql server specific details leak through in the mapping like this...
    – DanP
    Commented Sep 14, 2010 at 0:15
  • I believe there already is one: 216.121.112.228/browse/NH-2302 Incidentally...would you be interested in a donated domain name for the JIRA or something? Having an IP based url seems a little...inconvenient ;)
    – DanP
    Commented Sep 16, 2010 at 18:47
  • @DanP, the domain name is jira.nhforge.org, I have no clue why it's being using the IP for some months now. I don't administer it, but you can ask in the dev list. Commented Sep 16, 2010 at 21:12
  • 2
    The fluent equivalent is .CustomType("StringClob").CustomSqlType("nvarchar(max)") Commented Feb 23, 2012 at 22:18
  • what if you wanted to go against something other than sql server? No way to set the length of the field to -1 or something and let the dialect engine sort it out?
    – user610217
    Commented Mar 8, 2012 at 2:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.