Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate table per hierarchy mapping problem
    primarykey
    data
    text
    <p>I'm using Fluent NHibernate version 1.0.0.579 (latest version at this date). I have an abstract Activity class and several inheriting classes, eg. DummyActivity. All of them use the same table Activities, and all of them have a discriminator value based on an integral type which points to a mapping in the project (not a FK in database).</p> <p>We built the mapping like this:</p> <pre><code>public class ActivityMap : ClassMap&lt;Activity&gt; { public ActivityMap() { Table("Activities"); Id(x =&gt; x.Id).Column("ID").GeneratedBy.Guid(); Map(x =&gt; x.ActivityName).Not.Nullable().Length(50); HasMany(x =&gt; x.ActivityParameters) .KeyColumn("ActivityID") .AsMap&lt;string&gt;(idx =&gt; idx.Column("ParameterName"), elem =&gt; elem.Column("ParameterValue")) .Not.LazyLoad() .Cascade.Delete() .Table("ActivityParameters"); DiscriminateSubClassesOnColumn&lt;int&gt;("ActivityType") .SubClass&lt;DummyActivity&gt;(1, c =&gt; { }); } } </code></pre> <p>The generated hbm.xml file is:</p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"&gt; &lt;class xmlns="urn:nhibernate-mapping-2.2" name="***.Activity, ***, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Activities"&gt; &lt;id name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="ID" /&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;discriminator column="ActivityType" type="Int32" insert="true" not-null="true" /&gt; &lt;property name="ActivityName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="ActivityName" length="50" not-null="true" /&gt; &lt;/property&gt; &lt;map cascade="delete" lazy="false" name="ActivityParameters" table="ActivityParameters"&gt; &lt;key&gt; &lt;column name="ActivityID" /&gt; &lt;/key&gt; &lt;index type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="ParameterName" /&gt; &lt;/index&gt; &lt;element type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="ParameterValue" /&gt; &lt;/element&gt; &lt;/map&gt; &lt;subclass name="***.DummyActivity, ***, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="1" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>According to my belief, this looks like a valid hbm.xml file, identical in structure with the example given in the official NHibernate reference document, that is </p> <pre><code>&lt;class name="IPayment" table="PAYMENT"&gt; &lt;id name="Id" type="Int64" column="PAYMENT_ID"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;discriminator column="PAYMENT_TYPE" type="String"/&gt; &lt;property name="Amount" column="AMOUNT"/&gt; ... &lt;subclass name="CreditCardPayment" discriminator-value="CREDIT"&gt; ... &lt;/subclass&gt; &lt;subclass name="CashPayment" discriminator-value="CASH"&gt; ... &lt;/subclass&gt; &lt;subclass name="ChequePayment" discriminator-value="CHEQUE"&gt; ... &lt;/subclass&gt; &lt;/class&gt; </code></pre> <p>Are we making some mistake in our mapping? Also, can somebody point me the new implementation recommended by Fluent (using SubClass with discriminator column, something like</p> <pre><code>public class ActivityMap : ClassMap&lt;Activity&gt; { public ActivityMap() { Table("Activities"); Id(x =&gt; x.Id).Column("ID").GeneratedBy.Guid(); Map(x =&gt; x.ActivityName).Not.Nullable().Length(50); HasMany(x =&gt; x.ActivityParameters) .KeyColumn("ActivityID") .AsMap&lt;string&gt;(idx =&gt; idx.Column("ParameterName"), elem =&gt; elem.Column("ParameterValue")) .Not.LazyLoad() .Cascade.Delete() .Table("ActivityParameters"); DiscriminateSubClassesOnColumn&lt;int&gt;("ActivityType"); } } public class DummyActivityMap : SubClass&lt;DummyActivity&gt; { ///discriminator value here how??? } </code></pre> <p>?)</p> <p>The stack trace is </p> <pre><code>[FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal) +7469351 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119 NHibernate.Type.Int32Type.FromStringValue(String xml) +36 NHibernate.Type.Int32Type.StringToObject(String xml) +10 NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) +7824 [MappingException: Could not format discriminator value to SQL string of entity ***.Activity] NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) +8183 NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) +68 NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +1468 NHibernate.Cfg.Configuration.BuildSessionFactory() +87 FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:93 [FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. ] ***.Container.ConfigureNHibernate() in ***.Unity\Container.cs:92 ***.Container.ConfigureContainer() in ***.Unity\Container.cs:60 ***.Container.GetInstance() in ***.Unity\Container.cs:45 ***.Global.CreateContainer() in ***\Global.asax.cs:72 ***.Global.Application_Start(Object sender, EventArgs e) in ***\Global.asax.cs:44 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload