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

On Mon, Dec 19, 2011 at 7:05 PM, Mark Allison <[email protected]> wrote:

> 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>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Ramon

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

Reply via email to