Nhibernate is truncating strings before sending to database

680 views
Skip to first unread message

Mark Allison

unread,
Dec 19, 2011, 1:05:05 PM12/19/11
to nhusers
Hi,

I'm following along with http://summerofnhibernate.com/ and I'm up to
Session 04. I am trying to test for an ExpectedException when I insert
a string that is too big for the database column. I have a varchar(10)
column in the database and I'm inserting a 20-char string with this
code:

private Market BuildInvalidMarket()
{
return new Market("THIS IS TOO LONG FOR A SYMBOL","This
will not save");
}

[Test]
[ExpectedException(typeof(NHibernate.HibernateException))]
public void AddMarketThrowsExceptionOnFail()
{
_provider.AddMarket(BuildInvalidMarket());
}

Here's the method in the data access layer:

public void AddMarket(Market mkt)
{
using (ISession session = GetSession())
{
using (ITransaction tx = session.BeginTransaction())
{
try
{
session.Save(mkt);
session.Flush();
tx.Commit();
}
catch (HibernateException)
{
tx.Rollback();
throw;
}
}
}
}

The test fails because the string does get to the database, but it is
truncated. Why is Hibernate truncating the string before inserting it
to the database? I don't want it to do that.

Here's the database table:
CREATE TABLE [dbo].[markets](
[symbol] [varchar](10) NOT NULL,
[description] [varchar](30) NOT NULL,
CONSTRAINT [PK_markets] PRIMARY KEY CLUSTERED
(
[symbol] ASC
)

and the mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MooDB"
namespace="MooDB.BusinessLayer">

<class name="MooDB.BusinessLayer.Market" table="markets">
<id name="Symbol" column="symbol" type="string" length="10" />
<property name="Description" column="description"
type="string" length="30" not-null="true" />
</class>
</hibernate-mapping>

Ramon Smits

unread,
Dec 20, 2011, 4:11:55 AM12/20/11
to nhu...@googlegroups.com


I dont know for sure but you have another problem.

Your mapping says type="string" but your database type is varchar.

Either change the database type from varchar to nvarchar OR change the mapping type from type="string" to type="AnsiString".


Regards,
Ramon


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Ramon

Øyvind Valland

unread,
Dec 20, 2011, 4:30:38 AM12/20/11
to nhu...@googlegroups.com

Ramon Smits

unread,
Dec 20, 2011, 4:48:41 AM12/20/11
to nhu...@googlegroups.com

Mark Allison

unread,
Dec 20, 2011, 7:36:05 AM12/20/11
to nhu...@googlegroups.com
Thanks I changed my database schema to nvarchar, I should have done that in the first place really. Any idea on the truncation thing? 

Richard Brown (gmail)

unread,
Dec 20, 2011, 3:23:58 PM12/20/11
to nhu...@googlegroups.com

Mark Allison

unread,
Dec 20, 2011, 4:18:28 PM12/20/11
to nhu...@googlegroups.com
Thanks, I have Version: 3.2.0.4000. It seems the bug I am up against is fixed in 3.2.1. Is there a roadmap to find out when this will be released? 

Richard Brown

unread,
Dec 20, 2011, 5:46:32 PM12/20/11
to nhu...@googlegroups.com

I think we're already passed our original intended date, so I would imagine it'll be soon.

You can always download the latest zip for the NH trunk build from the TeamCity server.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
Reply all
Reply to author
Forward
0 new messages