NHibernate的CreateSQLQuery [英] NHibernate CreateSQLQuery

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

问题描述

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

Im trying to get some data with NH CreateSQLQuery method like

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

物流课程是

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

映射

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

但是我有错误

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?

推荐答案

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

An AliasToBean transformer 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).

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

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

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

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

return query.List<Logistic>();                                  

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

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

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

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