5

I'm executing a stored procedure using GetNamedQuery and setting a string parameter using SetString. NHibernate sets the string parameter to be an NVarchar(4000). My string parameter value is actually longer than this and so gets truncated.

Is there any way to tell NHibernate to use a longer string type when executing the query? The query is defined in the mapping file as simply. exec dbo.ProcessUploads :courseId, :uploadxml

Edit: neither of my parameters are properties of the enties involved.

Pete S
  • 265
  • 2
  • 11
  • Check this other question to see if helps http://stackoverflow.com/questions/6626394/how-does-one-make-nhibernate-stop-using-nvarchar4000-for-insert-parameter-stri – Claudio Redi Jun 29 '12 at 14:48

2 Answers2

13

Since NHibernate doesn't have enough information to set the parameter length automatically, you have to do it manually.

Example:

session.GetNamedQuery("ProcessUploads")
       .SetParameter("courseId", courseId)
       .SetParameter("uploadXml", uploadXml, NHibernateUtil.StringClob)
       .ExecuteUpdate();

In this case I'm using StringClob, which would translate to NVARCHAR(max).

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
1

You should show us or check your HBM file for parameters. There you can specify field/property type, lenght...

Here you can read about in official nHibernate documentation

Alex F
  • 3,180
  • 2
  • 28
  • 40
  • Just to add to this answer... NHibernate is using mapped property to figure out the length of parameter. More info on this question: http://stackoverflow.com/questions/4932841/nhibernate-setting-long-string-as-a-parameter-for-query – Miroslav Popovic Jun 29 '12 at 14:54
  • The parameters I'm setting aren't properties of the entity involved. The problematic one is an xml string which is processed by the stored proc. – Pete S Jun 29 '12 at 15:10