NHibernate-查询前刷新? [英] NHibernate - flush before querying?

查看:88
本文介绍了NHibernate-查询前刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储库类,该存储库类使用NHibernate会话将对象持久存储到数据库中.默认情况下,存储库不使用显式事务-由调用方管理.我有以下单元测试来测试我的NHibernate管道:

I have a repository class that uses an NHibernate session to persist objects to the database. By default, the repository doesn't use an explicit transaction - that's up to the caller to manage. I have the following unit test to test my NHibernate plumbing:

[Test]
public void NHibernate_BaseRepositoryProvidesRequiredMethods()
{            
    using (var unitOfWork = UnitOfWork.Create())
    {
        // test the add method
        TestRepo.Add(new TestObject() { Id = 1, Name = "Testerson" });
        TestRepo.Add(new TestObject() { Id = 2, Name = "Testerson2" });
        TestRepo.Add(new TestObject() { Id = 3, Name = "Testerson3" });

        // test the getall method
        var objects = TestRepo.GetAll();
        Assert.AreEqual(3, objects.Length);

        // test the remove method
        TestRepo.Remove(objects[1]);
        objects = TestRepo.GetAll();
        Assert.AreEqual(2, objects.Length);

        // test the get method
        var obj = TestRepo.Get(objects[1].Id);
        Assert.AreSame(objects[1], obj);
    }
}

问题是该行

Assert.AreEqual(3, objects.Length);

测试失败,因为从GetAll方法返回的对象列表为空.如果我在插入三个对象之后立即手动刷新了会话,则测试的那部分通过了.我在会话上使用默认的FlushMode,并且根据文档,应该在运行查询以检索所有对象之前将其刷新,但显然不是.我想念的是什么?

fails the test because the object list returned from the GetAll method is empty. If I manually flush the session right after inserting the three objects, that part of the test passes. I'm using the default FlushMode on the session, and according to the documentation, it's supposed to flush before running the query to retrieve all the objects, but it's obviously not. What I am missing?

如果这有任何区别,我正在将Sqlite用于单元测试方案.

I'm using Sqlite for the unit test scenario, if that makes any difference.

推荐答案

您声明

根据文档,应该在运行查询以检索所有对象之前刷新

according to the documentation, it's supposed to flush before running the query to retrieve all the objects

但是该文档位于 https://www.hibernate .org/hib_docs/v3/api/org/hibernate/FlushMode.html ,文档指出处于自动刷新模式(重点是我的):

But the doc at https://www.hibernate.org/hib_docs/v3/api/org/hibernate/FlushMode.html, the doc states that in AUTO flush mode (emphasis is mine):

会话被有时刷新 在执行查询之前,为了 确保查询永远不会返回过时 状态.这是默认的刷新模式.

The Session is sometimes flushed before query execution in order to ensure that queries never return stale state. This is the default flush mode.

因此,是的,您需要进行冲洗以保存这些值,然后才能期望它们出现在您的选择中.

So yes, you need to do a flush to save those values before expecting them to show up in your select.

这篇关于NHibernate-查询前刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆