2011-10-27 58 views
1

我已經用loquacious映射(通過代碼映射)替換了我所有的NHibernate xml映射文件。我想不通的唯一的事情是,如果有可能使用能說會道的映射來定義這個命名查詢:如何使用Nhibernate的loquacious映射將SQL函數映射爲命名查詢?

<?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> 

有誰知道這是可能的,所以如何做到這一點或點我在正確的方向?

由於提前, 問候,泰德

回答

1

您仍然可以混合您的映射,即使用映射的所有新多汁的代碼,仍然有一些你HBM命名的映射文件。

解決的辦法很簡單,首先你需要定義你的web.config(或外部NHibernate的配置文件)爲: -

<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的: -

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

我對此寫了一個blog post

+0

嗨Rippo,對不起,我忘了提,但這正是我現在正在做的..我只是想知道如果我可以完全丟失的XML文件。順便說一句:因爲你的博客,我知道了!幾周前已經發現它..所以感謝博客! – TedOnTheNet

+0

啊! :)好博客:) – Rippo

+0

如果你找到解決方案,你會發布在這裏?我還沒有自己的工作呢 – Rippo