nhibernate动态绑定一个类 [英] nhibernate dynamically bind a class

查看:98
本文介绍了nhibernate动态绑定一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读本文,并正在寻找一种方法,该方法可以在运行时动态更改我的映射,以使用一对多依赖于父对象中的值的方法绑定到其他表.

这是我的映射

 <bag name="Data" mutable="true" >
     <key>
       <column name="Log_Link" />
       <column name="channel" />
     </key>
   <one-to-many class="Fluent.Entities.Meters.FTIMeterChannelData, Poco" entity-name="30" />
 </bag>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Fluent.Entities.Meters.FTIMeterChannelData, Poco" table="loggerData" entity-name="30">
    <composite-id mapped="false" unsaved-value="undefined">
      <key-property name="Channel" type="System.Int32">
        <column name="channel" />
      </key-property>
      <key-property name="LogLink" type="System.Int32">
        <column name="Log_Link" />
      </key-property>
    </composite-id>
    <property name="Date" type="System.DateTime">
      <column name="hhdate" />
    </property>
  </class>

  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Fluent.Entities.Meters.FTIMeterChannelData, Poco" table="loggerData10" entity-name="15">
    <composite-id mapped="false" unsaved-value="undefined">
      <key-property name="Channel" type="System.Int32">
        <column name="channel" />
      </key-property>
      <key-property name="LogLink" type="System.Int32">
        <column name="Log_Link" />
      </key-property>
    </composite-id>
    <property name="ReadingType" type="System.Char">
      <column name="readingtype" />
    </property>
  </class>
</hibernate-mapping>

现在如文章所述,我应该能够使用拦截器来更改实体名称

 class LoggerDataInterceptor : EmptyInterceptor
    {
        public override string GetEntityName(object entity)
        {
            return Convert.ToString("20");
        }
    }

现在的问题有两个.

首先,尽管我在打开会话时就宣布了拦截器,但我似乎还是无法启动它;其次,我完全狂吠了,这永远不会起作用吗?

解决方案

我可以对第一个问题发表评论.打开会话时您正在通过拦截器吗?

config.SetInterceptor(new yourInteceptor());

.........

if (config.Interceptor != null)
{
     session = factory.OpenSession(config.Interceptor);
}
     else
{    
     session = factory.OpenSession();
}

I've read this article, and am looking for a way to dynamically change my mapping at runtime to bind to a different table using a one-to-many dependent on a value in my parent object.

Here are my mappings

 <bag name="Data" mutable="true" >
     <key>
       <column name="Log_Link" />
       <column name="channel" />
     </key>
   <one-to-many class="Fluent.Entities.Meters.FTIMeterChannelData, Poco" entity-name="30" />
 </bag>

and

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Fluent.Entities.Meters.FTIMeterChannelData, Poco" table="loggerData" entity-name="30">
    <composite-id mapped="false" unsaved-value="undefined">
      <key-property name="Channel" type="System.Int32">
        <column name="channel" />
      </key-property>
      <key-property name="LogLink" type="System.Int32">
        <column name="Log_Link" />
      </key-property>
    </composite-id>
    <property name="Date" type="System.DateTime">
      <column name="hhdate" />
    </property>
  </class>

  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Fluent.Entities.Meters.FTIMeterChannelData, Poco" table="loggerData10" entity-name="15">
    <composite-id mapped="false" unsaved-value="undefined">
      <key-property name="Channel" type="System.Int32">
        <column name="channel" />
      </key-property>
      <key-property name="LogLink" type="System.Int32">
        <column name="Log_Link" />
      </key-property>
    </composite-id>
    <property name="ReadingType" type="System.Char">
      <column name="readingtype" />
    </property>
  </class>
</hibernate-mapping>

now as the article states I should be able to change the entity name using an interceptor

 class LoggerDataInterceptor : EmptyInterceptor
    {
        public override string GetEntityName(object entity)
        {
            return Convert.ToString("20");
        }
    }

Now the question is two fold.

Firstly I can't seem to get this interceptor to fire, despite declaring it when opening my session, and secondly am I completely barking mad and is this never going to work?

解决方案

I can comment on the first question. Are you passing the interceptor when you open the session?

config.SetInterceptor(new yourInteceptor());

.........

if (config.Interceptor != null)
{
     session = factory.OpenSession(config.Interceptor);
}
     else
{    
     session = factory.OpenSession();
}

这篇关于nhibernate动态绑定一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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