开发者

Why NHibernate auto-truncates instead of throwing an exception on save?

开发者 https://www.devze.com 2023-01-11 16:48 出处:网络
I have been studying the Summer Of NHibernate Tutorials and when I have reached the Session 04: Exploring Transactions and Concurrency -just like in the tutorial- I wanted to create a SAVE test which

I have been studying the Summer Of NHibernate Tutorials and when I have reached the Session 04: Exploring Transactions and Concurrency -just like in the tutorial- I wanted to create a SAVE test which should fail because of an invalid object.

The purpose is to test if it can rollback the transaction properly.

Idea is to create an invalid object that has a property with more chars than the required amount set by Db. But what NHibernate does is it gets the first required amount of chars and cuts the rest and saves it into Db successfully.

Here is my Test which does not fail:

   [Test]
public void AddCountryThrowExceptionOnFail()
{
    // This returns a Country with country code having more than 50 chars 
    // Where its length is set to 3 chars only          
    Country invalidCountry = GetInvalidCountry();
    _dataProvider.AddCountry(invalidCountry);
}

This is the AddCountry method:

public int AddCountry(Country country)
{
using (ISession session = GetSession())
{
    using (ITransaction tx = session.BeginTransaction())
    {
        try
        {
            int pk_Country = (int)session.Save(country);
            session.Flush();

            tx.Commit();

            return pk_Country;
        }
        catch(Exception)
        {
            tx.Rollback();
            throw;
        }
    }
}
}

And this is how the mapping file is set for CountryCode property:

<property name="CountryCode" column="CountryCode" type="string" length="3" not-null="true"></property>

So, question is, why NHibe开发者_如何学Crnate gets the first N chars where n=length set in the mapping file? How can I prevent this to happen so that it can fail on save?

Thanks, burak ozdogan


It is not NHiberante that automatically truncates, it is your database. You can try different databases and see that some truncate, and some don't and others are configurable. An exception is thrown by Nhiberante when a too long string is send to a database that does not truncate.


If you remove

length="3"

it should be trowing an exception

0

精彩评论

暂无评论...
验证码 换一张
取 消