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

时间:2012-04-11 11:08:17

标签: c# nhibernate stored-procedures fluent-nhibernate fluent-nhibernate-mapping

我使用ISession.CreateSQLQuery调用存储过程。

然后我用

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

此方法的问题在于AliasToBeanResultTransformer仅将Article表一对一地映射到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();
    }
}

但我的Article表上的主键名为ArticleId,因此AliasToBeanResultTransformer会抛出异常。

  

无法在课程中找到属性'ArticleId'的setter   'Core.DomainModels.Article'

使用CreateSqlQuery时有没有办法重用FluentNhibernateMapping?

编辑:

Nhibernate Documentation描述了如何将已映射的实体与hbm文件一起使用。

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

我真的问自己为什么不能仅通过代码来做到这一点?!

2 个答案:

答案 0 :(得分:3)

似乎无法使用ISession.CreateSQLQuery并从ISession获取映射的实体。

我现在所做的是将hbm文件和FluentNhibernate映射一起使用。

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:

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

配置:

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

存储库:

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

答案 1 :(得分:3)

我正在使用此代码映射到存储库中的实体。我相信它也适用于调用程序:

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