C# NHibernate CreateSQLQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12513191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:30:40  来源:igfitidea点击:

NHibernate CreateSQLQuery

c#nhibernate

提问by maxs87

Im trying to get some data with NH CreateSQLQuery method like

我正在尝试使用 NH CreateSQLQuery 方法获取一些数据,例如

IList<Logistic> LCollection = sess.CreateSQLQuery(@"select * from some_schema.logistic")
                                           .SetResultTransformer(Transformers.AliasToBean(typeof(Logistic)))
                                           .List<Logistic>();

logistic class is

物流类是

public class Logistic
{
    public virtual long? l_id { get; set; }
    public virtual long? carrier_id { get; set; }
    ...
}

mapping

映射

public class LogisticMap : ClassMap<Logistic>
{
    public LogisticMap()
    {
        Table("some_chema.logistic");
        Id(x => x.l_id).GeneratedBy.Sequence("some_chema.logistic_sq");
        Map(x => x.carrier_id);
        ...
    }
}

but i have the error

但我有错误

The type System.Decimal can not be assigned to a property of type System.Nullable`1[System.Int64] setter of MyNamespase.Logistic.l_id

any idea what may be wrong?

知道可能有什么问题吗?

采纳答案by Frederik Gheysels

An AliasToBeantransformer is used when you want to retrieve lightweight DTO's instead of entities. (For instance, if you have an overview-screen, which displays only some essential information of each entity, then it is better to use a DTO and create a query in NHibernate which uses an AliasToBean transformer so that NH knows that it shouldn't retrieve the complete entities).

AliasToBean,当你想要检索轻量级DTO的,而不是实体变压器使用。(例如,如果您有一个概览屏幕,它只显示每个实体的一些基本信息,那么最好使用 DTO 并在 NHibernate 中创建一个使用 AliasToBean 转换器的查询,以便 NH 知道它不应该检索完整的实体)。

If you want to retrieve entities using a SQL query, you'll have to do it like this:

如果要使用 SQL 查询检索实体,则必须这样做:

var query = sess.CreateSQLQuery(@"select {l.*} from some_schema.logistic as l");

query.AddEntity ("l", typeof(Logistic));

return query.List<Logistic>();                                  

But, I wonder why you'd want to use a native SQL query in this case ? Why not use HQL, ICriteriaor QueryOver?

但是,我想知道您为什么要在这种情况下使用本机 SQL 查询?为什么不使用HQL,ICriteriaQueryOver