简体   繁体   English

NHibernate CreateSQLQuery

[英]NHibernate CreateSQLQuery

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? 什么可能是错的?

An AliasToBean transformer is used when you want to retrieve lightweight DTO's instead of entities. 当您想要检索轻量级DTO而不是实体时,使用AliasToBean转换器。 (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). (例如,如果你有一个概览屏幕,它只显示每个实体的一些基本信息,那么最好使用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 ? 但是,我想知道为什么你想在这种情况下使用本机SQL查询? Why not use HQL , ICriteria or QueryOver ? 为什么不使用HQLICriteriaQueryOver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM