查询℃之间NHibernate的差异;吨>中获取< T>和负载< T> [英] NHibernate difference between Query<T>, Get<T> and Load<T>

查看:135
本文介绍了查询℃之间NHibernate的差异;吨>中获取< T>和负载< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到用做这样的事情时,从数据库中获取数据,这三种方式的区别:

I'd like to find the difference in using these three ways of getting data from the database when doing something like:

public T GetById(int id) {
    using (var db = Database.Session) {
        using (var t = db.BeginTransaction()) {
            try {
                return db.Get<T>(id);
            }
            catch (Exception) {
                if (!t.WasCommitted) {
                    t.Rollback();
                }
                throw;
            }
        }
    }
}

public T GetById(int id) {
    using (var db = Database.Session) {
        using (var t = db.BeginTransaction()) {
            try {
                return Query<T>().First(x=>x.Id == id);
                //or something like
                //return Query<T>().Where(x=>x.Id == id).FirstOrDefault();
                //or
                //return QueryOver<T>().Where(x=>x.Id == id).FirstOrDefault;
            }
            catch (Exception) {
                if (!t.WasCommitted) {
                    t.Rollback();
                }
                throw;
            }
        }
    }
}

或即使是这样的:

public T GetById(int id) {
    using (var db = Database.Session) {
        using (var t = db.BeginTransaction()) {
            try {
                return db.Load<T>(id);
            }
            catch (Exception) {
                if (!t.WasCommitted) {
                    t.Rollback();
                }
                throw;
            }
        }
    }
}

和加入另外一个问题这一个,怎么查询()不同于 QueryOver()

And adding another question to this one, how the Query() differs from QueryOver()?

我已经在这里读计算器一些答案,但由于他们中的绝大多数是关于使用LINQ,和NHibernate 3的开始,我想知道今天怎么是该方案

推荐答案

类似的试图解释这是这里

在一般我们有三种方式可以由它的编号来获得从数据库实例。

In general we've got three ways how to get an instance from DB by its ID.

1)查询 - 这就是我们使用QueryOver API(本地NHibernate的语言)或查询(实现MS LINQ的API)。这些查询总是打DB(或高速缓存),并可以加载全根对象,获取一些关系,或只是预测(只有几列/属性转换成一些DTO)

1) Querying - That is where we use QueryOver API (native NHibernate language) or Query (implementation of the MS LINQ API). These queries always hit the DB (or cache) and could load full root object, fetch some relations, or be just projections (only few columns/properties converted into some DTO)

2)然后,我们的 获取LT; TEntity>() ,它的目的是作为最常见的方式如何通过ID来获得项目。它总是击中DB,因为它的合同说(的 GET ):

2) Then, we have Get<TEntity>(), which was intended as the most common way how to get item by ID. It always hits DB because its contract says (get):

返回给定的实体类的持久化实例与给定的标识符,或无效如果不存在这样的持久化实例

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance

因此,要确保对象存在与否,DB必须按

So, to be sure that object exists or not, DB must be hit.

3)最后,还有第三的合同 - 加载< TEntity>()。它永远不会打DB来检查是否有这样一个项目的(以提供的ID)的:的 load()的

3) Finally, there is a third contract - Load<TEntity>(). It will never hit DB to check if there is such an item (with provided ID): load():

返回给定的标识符指定的实体类的持久化实例,假设实例存在。

Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

如果我们已经有了一个参考编号,我们知道它确实存在,我们应该使用加载< TEntity>()

If we've got a reference ID, and we know that it does exist we should use the Load<TEntity>().

它将只是创建一个代理 - 与提供的ID 和根/持有人的INSERT或UPDATE时实体,代理ID将被用于创建正确的SQL语句

It will just create a proxy - with provided ID and during the INSERT or UPDATE of that root/holder entity, the proxy ID will be used to create correct SQL Statement.

摘要: 的get()加载()在那里我们是有原因的。它们被设计为支持不同的方案。

Summary: Get() and Load() are there for us for a reason. They were designed to support different scenarios.

另请参阅:

  • Different between session.get() and session.load()
  • NHibernate – The difference between Get, Load and querying by id

这篇关于查询℃之间NHibernate的差异;吨&gt;中获取&LT; T&GT;和负载&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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