1

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

TedOnTheNet
  • 1,082
  • 1
  • 8
  • 23

1 Answers1

1

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.

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> 

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

I have written a blog post regarding this.

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Hi Rippo, I'm sorry I forgot to mention, but that's exactly what I have right now.. I was just wondering if I could lose the xml files completely. btw: I got that for because of your blog! already found it a few weeks ago.. so thanks for blogging! – TedOnTheNet Oct 27 '11 at 15:39
  • If you do find a solution will you post it here? I haven't worked this out myself yet – Rippo Oct 27 '11 at 18:13
  • I will.. I learned quite a lot about NHibernate the past year. – TedOnTheNet Oct 28 '11 at 07:25
  • The named query I had was the only thing left in xml format and I decided to execute it with a CreateSqlQuery on the session. it's not more fault-prone than using XML, both can contain typo's. I covered it with some unittests. That way I got rid of the xml mapping configuration. – TedOnTheNet Nov 10 '11 at 14:58