Binary Blob truncated to 8000 bytes - SQL Server 2008 / varbinary(max)
Asked Answered
D

5

23

I have upgraded from Fluent Nhibernate 1.0 with Nhibernate 2.1 to pre- release 1.x with NHibernate 3.0 GA and have hit what I think is a regression, but I want to hear if that's indeed the case.

I am using SQL Server Express 2008 and the MSSQL 2008 dialect and have an Image property of type System.Drawing.Image and I have mapped it like this:

Map (food => food.Image)
 .Length (int.MaxValue)
 .Nullable ();

The Image column in the table is of type varbinary(MAX).

The generated hbm for the property is:

<property name="Image" type="System.Drawing.Image, System.Drawing,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
   <column name="Image" length="2147483647" not-null="false" />
</property>`

However no matter what I do the binary blob is truncated to 8000 bytes when serialized with the current FNH and NH versions. That didn't used to be the case with previous versions.

Ideas of why this is happening and how to fix/workaround it?

Dorena answered 3/1, 2011 at 11:52 Comment(0)
D
4

This is a regression. I have raised a bug and provided patches at https://nhibernate.jira.com/browse/NH-2484

Dorena answered 14/1, 2011 at 16:10 Comment(3)
I have the same problem with NHibernate 3.1.0.4000. Am I doing something wrong?Vedetta
I don't know - I use a patched 3.0 and can't update to 3.1 to test right now due to other deps on 3.0. Easiest would be to take my test case from the bug report, replace 3.0 with 3.1 and see if it still throws. If it does we should reopen the defect and temporally you can use a custom UserType for Image which forces the size of the data.Dorena
After more testing, I found NH 3.1.0 is not buggy anymore. I was using corrupted data (saved with 8000 bytes) of another version, then I had difficulties to find out the bug cause. It's ok now. Thank you!Vedetta
L
22

I too have encountered a similar problem and after much experimentation I noticed that when using Nhibernate to generate my schema to a file the generated column type was always length 8000.

Setting setting CustomSqlType to Varbinary(max) as suggested above made no difference, however, this work around in my FluentMapping seemed to do the trick:

Map(x => x.LogoBytes).CustomType("BinaryBlob").Length(1048576).Nullable();  

The length of course is an arbitrary amount but I think it should be set to something less than int.Max. I am new to Nhibernate so I'm still figuring things out but I'd be interested to know if this helps you.

Loewi answered 4/2, 2011 at 20:47 Comment(5)
This corrected it for me; however, I just used int.MaxValue for the Length so as not to impose any low limits.Carlisle
As of nhibernate 3.2.0.4000 and fluentnhibernate 1.3.0.717 i still had to use this to get past the 8000 limit.Mercuric
The solution worked for me too. As I'm using NH 3.2.400 and Mapping by code, I used following: map.Property(x => x.Image, status => status.Column(c => { c.SqlType("VARBINARY(MAX)"); c.Length(int.MaxValue); }));Metaphysic
I've found simply setting length to int.MaxValue is enough to solve this issue.Marocain
The issue reported in NHibernate JIRA indicates that they won't fix this, but you rather need to specify the length manually, so this is the correct approach to the problem.Mercedezmerceer
U
5

In 3.0.0GA, the following mapping seems to do the trick:

        <property name="Data" type="Serializable" length="2147483647" />
Upturned answered 31/1, 2011 at 7:26 Comment(0)
D
4

This is a regression. I have raised a bug and provided patches at https://nhibernate.jira.com/browse/NH-2484

Dorena answered 14/1, 2011 at 16:10 Comment(3)
I have the same problem with NHibernate 3.1.0.4000. Am I doing something wrong?Vedetta
I don't know - I use a patched 3.0 and can't update to 3.1 to test right now due to other deps on 3.0. Easiest would be to take my test case from the bug report, replace 3.0 with 3.1 and see if it still throws. If it does we should reopen the defect and temporally you can use a custom UserType for Image which forces the size of the data.Dorena
After more testing, I found NH 3.1.0 is not buggy anymore. I was using corrupted data (saved with 8000 bytes) of another version, then I had difficulties to find out the bug cause. It's ok now. Thank you!Vedetta
C
1

Map(x => x.Image).Length(100000).Not.Nullable();

Add the 'Length(MAXVALUE)' as above and it will work :)

Chicalote answered 19/8, 2011 at 11:47 Comment(0)
S
0

Have you tried this?

Map(x => x.Image).CustomSqlType("VARBINARY(MAX)");
Saucy answered 3/1, 2011 at 13:16 Comment(1)
Yes I have tried it and it makes no difference. I have checked with the Fluent NHibernate guys and it's not a regression on their side so I have raised an nhibernate issue.Dorena

© 2022 - 2024 — McMap. All rights reserved.