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>
--
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.
Have a look at this StackOverflow question
http://stackoverflow.com/questions/3701907/how-to-store-a-non-truncated-varcharmax-string-with-nhibernate-and-fluent-nhib
and this referenced link:
http://www.primordialcode.com/blog/post/nhibernate-prepare_sql-considerations-mapping-long-string-fields
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.