开发者

nhibernate dynamically bind a class

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">
        <col开发者_JS百科umn 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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜