如何使用 Nhibernate 的 loquacious 映射将 sql 函数映射为命名查询?

发布于 2024-12-12 15:11:12 字数 652 浏览 4 评论 0原文

我已将所有 NHibernate xml 映射文件替换为冗长的映射(通过代码映射)。我唯一不知道的是是否可以使用冗长的映射来定义这个命名查询:

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

有谁知道这是否可能,那么如何做到这一点或为我指明正确的方向?

提前致谢, 问候,特德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-12-19 15:11:13

您仍然可以混合映射,即通过代码使用映射的所有新功能,并且仍然拥有一些 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();

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文