为什么 NHibernate 会自动截断而不是在保存时抛出异常?

发布于 2024-09-14 14:58:02 字数 1319 浏览 1 评论 0原文

我一直在学习 NHibernate 教程的夏季课程,当我完成第 04 节:探索事务和并发时(就像在教程中一样),我想创建一个 SAVE 测试,该测试应该因无效对象而失败。

目的是测试是否可以正常回滚事务。

想法是创建一个无效对象,该对象的属性的字符数多于 Db 设置的所需数量。但是NHibernate所做的是它获取第一个所需数量的字符并剪切其余的字符并将其成功保存到Db中

这是我的测试,它不会失败:

   [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);
}

这是 AddCountry 方法:

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;
        }
    }
}
}

这就是为 CountryCode 属性设置映射文件的方式:

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

那么,问题是,为什么 NHibernate 会获取映射文件中设置的 n=length 的前 N ​​个字符?如何防止这种情况发生,以免保存失败?

谢谢, 布拉克·奥斯多安

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 NHibernate 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍白女子 2024-09-21 14:58:02

自动截断的不是 NHiberante,而是您的数据库。您可以尝试不同的数据库,看看有些会截断,有些不会,有些是可配置的。当将太长的字符串发送到不截断的数据库时,Nhiberante 会引发异常。

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.

妄断弥空 2024-09-21 14:58:02

如果删除

length="3"

它应该会引发异常

If you remove

length="3"

it should be trowing an exception

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文