如何使用Nhibernate的无缝映射将sql函数映射为命名查询? [英] How to map a sql function as named query with Nhibernate's loquacious mapping?

查看:124
本文介绍了如何使用Nhibernate的无缝映射将sql函数映射为命名查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用过时的映射(通过代码映射)替换了我所有的NHibernate xml映射文件.我唯一不知道的是,是否有可能使用过时的映射定义此命名查询:

I have replaced all my NHibernate xml mapping files by loquacious mappings (mapping by code). The only thing I can't figure out is if it is possible to define this named query using loquacious mappings:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Domain" assembly="Domain" xmlns="urn:nhibernate-mapping-2.2"> 
  <sql-query name="MyFunction">
    <query-param name="Inputparam1" type="Int32"/>
    <query-param name="Inputparam2" type="Int32"/>
    <return-scalar column="ResultParam" type="Int32"/>
    <![CDATA[ select MyFunction(:Inputparam1,:Inputparam2) as ResultParam ]]>
  </sql-query>
</hibernate-mapping>

有人知道这是否可行吗?怎么做或将我指向正确的方向?

Does anyone know if it's possible, and so how to do it or point me in the right direction?

预先感谢, 问候,泰德

Thanks in advance, Regards, Ted

推荐答案

您仍然可以混合使用映射,即使用代码映射的所有新功能,并保留一些HBM命名的映射文件.

You can still mix your mappings, that is use all the new juiciness of mapping by code and still have some of your HBM named mapping files.

解决方案非常简单,首先需要将web.config(或外部nhibernate配置文件)定义为:-

The solution is quite simple, first you need to define your web.config (or external nhibernate config file) as:-

<configSections>  
  <section name="hibernate-configuration"  
   type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
     requirePermission="false" />  
</configSections>  

<hibernate-configuration  
   xmlns="urn:nhibernate-configuration-2.2">  
  <session-factory>  
    <property name="dialect">  
      NHibernate.Dialect.MySQL5Dialect  
    </property>  
    <mapping assembly="Domain.Model" />  
  </session-factory>  
</hibernate-configuration> 

然后相应地配置NHibernate:-

Then configure NHibernate accordingly:-

var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());
//Notice the .Configure, this is the magic that allows you to
//  use the mixed mappings
var configure = new Configuration().Configure();
configure.DataBaseIntegration(x =>
{
  x.Dialect<MySQL5Dialect>();
  x.ConnectionStringName = "db";
}).CurrentSessionContext<WebSessionContext>();

configure.AddDeserializedMapping(mapping, "Domain");
SessionFactory = configure.BuildSessionFactory();

我写了一个博客文章关于此.

这篇关于如何使用Nhibernate的无缝映射将sql函数映射为命名查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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