流利NHibernate与常量的引用 [英] Fluent NHibernate References with constants

查看:105
本文介绍了流利NHibernate与常量的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Fluent NHibernate中映射的表。此表必须连接到ID上的另一个表,但也必须根据一组常量值过滤该表上的连接值。考虑下面的SQL:

pre $ SELECT
FROM
Table1
INNER JOIN
Table2 ON
Table1.Table2Id = Table2.Id
AND Table2.Category ='常量表达式'
AND Table2.Language ='常量表达式'



我对Table1的流畅映射现在看起来像这样:

  References(a => a.Table2).Nullable()。Columns(Table2Id)。ReadOnly(); 

如何实现常量表达式?

session.Get< Table1>()来执行此操作。即使你改变了映射,所以它是这样的:

pre $ References(a => a.Table2).Nullable ).Columns( Table2Id)只读()Fetch.Join。;

在输出的sql中,最有可能的是左外连接。唯一能够强制使用内部连接进行读取的方式(不需要下载额外的NHibernate插件)就可以使用 session.QueryOver (你也可以用session.Query和NHibernate的linq扩展)。如果是这种情况,那么你可以在QueryOver查询中指定一组常量。

  public class GetTableQuery 
{
private readonly string _category;
私有只读字符串_language;
$ b $ public GetTableQuery(string category,string language)
{
_category = category;
_language = language;
}

public IEnumerable< Table1> Execute(ISession session)
{
var returnList = session.QueryOver< Table1>()
.Inner.JoinQueryOver(t1 => t1.Table2)
.Where(t2 => t2.Category == _category& t2.Language == _language)
.List();

return returnList;




$ b我认为Russ所显示的ApplyFilter方法确实使得模型检索要简单得多,而且对于代码的可重用性(如果你有其他具有类别和语言的表)是非常好的,但是因为你的表引用是可以为空的引用,所以你必须使用查询。也许QueryOver与过滤器的组合是最好的(假设你可以得到流利的NHibernate的更新版本)


I have a table mapped in Fluent NHibernate. This table must join to another table on an ID, but must also filter the joined values on that table against a set of constant values. Consider the following SQL:

SELECT * 
FROM 
    Table1 
INNER JOIN 
    Table2 ON 
    Table1.Table2Id = Table2.Id 
    AND Table2.Category = 'A constant expression' 
    AND Table2.Language = 'A constant expression'

My fluent mapping for Table1 currently looks like this:

References(a => a.Table2).Nullable().Columns("Table2Id").ReadOnly();

How can I implement the constant expressions?

解决方案

I have noticed that your mapping specifies Nullable and no eager fetching (by default it will be lazy loaded). So if you did want to generate the sql you have shown in your comment, you would not be able to do it with a simple session.Get<Table1>(). Even if you changed the mapping so that it was like this :

References(a => a.Table2).Nullable().Columns("Table2Id").ReadOnly().Fetch.Join;

You would most likely end up with a left outer join in the outputted sql. The only way you would be able to force a fetch with inner join (without downloading any extra NHibernate addons) would be to use a session.QueryOver (you may also be able to do this with session.Query and the NHibernate linq extensions). If this is the case, then you may as well specify your set of constants inside the QueryOver query.

public class GetTableQuery
{
    private readonly string _category;
    private readonly string _language;

    public GetTableQuery(string category, string language)
    {
        _category = category;
        _language = language;
    }

    public IEnumerable<Table1> Execute(ISession session)
    {
        var returnList = session.QueryOver<Table1>()
            .Inner.JoinQueryOver(t1 => t1.Table2)
            .Where(t2 => t2.Category == _category && t2.Language == _language)
            .List();

        return returnList;
    }
}

I think the ApplyFilter method shown by Russ does make the model retrieval much simpler, and its really good for code re-usability (if you have other tables with categories and languages), but since your table reference is a nullable reference, you have to use a query anyway. Maybe a combination of QueryOver with the filter would be the best (assuming you can get the later version of fluent NHibernate)

这篇关于流利NHibernate与常量的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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