简体   繁体   English

Nhibernate:使用已定义的映射使用CreateSqlQuery调用存储过程

[英]Nhibernate: invoke stored procedure with CreateSqlQuery using already defined mapping

I Invoke a stored procedure using ISession.CreateSQLQuery . 我使用ISession.CreateSQLQuery调用存储过程。

Then I use 然后我用

SetResultTransformer(new AliasToBeanResultTransformer(typeof(Article))).List<Article>().ToList()

The problem with this approach is that the AliasToBeanResultTransformer only maps the Article table to the Article class one to one. 此方法的问题在于AliasToBeanResultTransformer仅将ArticleAliasToBeanResultTransformer映射到Article类。

public class Article : Entity
{
    public virtual string Description { get; set; }
}

public class Entity
{
    public virtual int Id { get; set; }
}

public class ArticleRepository : Repository<Article>, IArticleRepository
{
    private ISession _session;

    public ArticleRepository(ISession session) : base(session)
    {
        _session = session;
    }

    public List<Article> GetByDescription(string description)
    {
        return _session
            .CreateSQLQuery("EXEC ArticlesByDescription :Description")
            .SetString("Description", description)
            .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Article)))
            .List<Article>().ToList();
    }
}

But my primary key on my Article table is called ArticleId , so that the AliasToBeanResultTransformer throws an exception. 但是我的Article表上的主键叫做ArticleId ,因此AliasToBeanResultTransformer会抛出异常。

Could not find a setter for property 'ArticleId' in class 'Core.DomainModels.Article' 无法在“Core.DomainModels.Article”类中找到属性“ArticleId”的setter

Is there a way of reusing the FluentNhibernateMapping when using CreateSqlQuery ? 有没有使用时重用FluentNhibernateMapping的方式CreateSqlQuery

EDIT: 编辑:

The Nhibernate Documentation describes how you can use an already mapped entity with hbm files. Nhibernate文档描述了如何使用已映射的实体和hbm文件。

<sql-query name="GetProductsByCategoryId">
   <return class="Product" />
   exec dbo.GetProductsByCategoryId :CategoryId
</sql-query>

I really ask myself why is it not possible to do this just by code?! 我真的问自己为什么不能通过代码来做到这一点?!

It seems not possible to use ISession.CreateSQLQuery and get a mapped entity back from the ISession . 这似乎不可能使用ISession.CreateSQLQuery ,并得到一个映射实体从后面ISession

What I do for now is using hbm files and FluentNhibernate mappings together. 我现在要做的是将hbm文件和FluentNhibernate映射一起使用。

Hbm: HBM:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.DomainModels">
  <sql-query name="ArticlesByDescription">
    <return class="Article" />
    EXEC ArticlesByDescription :Description
  </sql-query>
</hibernate-mapping>

FluentNhibernate: FluentNhibernate:

public class ArticleMapping : ClassMap<Article>
{
    public ArticleMapping()
    {
        Id(x => x.ArticleId).GeneratedBy.Identity();
        Map(x => x.Description).UniqueKey("Article_Description_Unique");
    }
}

Configuration: 组态:

public class ConfigurationFactory
{
    const string Database = "";
    const string Server = "";

    public static Configuration Build()
    {           
        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                c => c.Database(Database).TrustedConnection().Server(Server)))
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssemblyOf<ArticleMapping>();
                m.HbmMappings.AddFromAssemblyOf<ArticleMapping>();
            })
            //.ExposeConfiguration(c => new SchemaExport(c).Execute(true, true, false))
            .BuildConfiguration();
    }
}

Repository: 库:

public class ArticleRepository : Repository<Article>, IArticleRepository
{
    private ISession _session;

    public ArticleRepository(ISession session) : base(session)
    {
        _session = session;
    }

    public List<Article> GetByDescription(string description)
    {
        return _session
            .GetNamedQuery("ArticlesByDescription")
            .SetString("Description", description)
            .List<Article>().ToList();
    }
}

I am using this code for mapping to entity in repository. 我正在使用此代码映射到存储库中的实体。 I believe it would work for calling procedure also: 我相信它也适用于调用程序:

public IEnumerable<Address> Search(string query, int maxCount)
{
    return session.CreateSQLQuery("SELECT * FROM address WHERE fts_col @@ plainto_tsquery('cs', :query) LIMIT :limit;")
        .AddEntity(typeof(Address)) // this is probably what you need to map to entity
        .SetString("query", query)
        .SetInt32("limit", maxCount)
        .List<Address>();
}

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

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