I have the following db tables which I have simplified for this example:
Webpage Id, URLIdentifier
WebpageMetaData Webpage_id Keywords
Webpage_id is the primary key for the table and a foriegn key back to the webpage table.
I am then using Fluent NHibernate and its automapping feature to map these to the following POCO classes:
public class Webpage
{
public virtual int Id { get; set; }
public virtual string UrlIdentifier { get; set; }
public virtual WebpageMetaData WebpageMetaData { get; set; }
}
public class WebpageMetaData
{
public virtual int Id { get; set; }
public virtual string Keywords{ get; set; }
public virtual Webpage Webpage { get; set; }
}
I want to map these entities as one-to-one, so I override the automapping as follows:
public class WebpageMap : IAutoMappingOverride<Webpage>
{
public void Override(AutoMapping<Webpage> mapping)
{
mapping.HasOne(w => w.WebpageMetaData)
.Not.Constrained()
.Fetch.Join()
.Cascade.All();
}
}
public class WebpageMetaDataMap : IAutoMappingOverride<WebpageMetaData>
{
public void Override(AutoMapping<WebpageMetaData> mapping)
{
mapping.Id(w => w.Id, "Webpage_id").GeneratedBy.Foreign("Webpage");
mapping.HasOne(w => w.Webpage)
.ForeignKey("Webpage_id")
.Constrained()
.Fetch.Join()
.Cascade.None();
}
}
I have tested these mappings and cascade rules and they work for selecting, inserting and updating. However when i perform a delete of the WebpageMetaData I get an exception saying that
"deleted object would be re-saved by cascade (remove deleted object from associations)[WebpageMetaData#524]"
The only way to stop the exception is by doing the following before saving:
webpageMetaData.Webpage.WebpageMetaData = null;
So I have to set a null reference to myself from my parent??! This seems wrong? have I got my mapping wrong?
Thanks.
Update 1: Please see the NH mappings that FLuent is producing, in case this is of help to the hard core NHibernator's :)
<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" name="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Webpage`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="Id" />
<generator class="identity" />
</id>
<one-to-one cascade="all" class="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="false" fetch="join" name="WebpageMetaData" />
<property name="UrlIdentifier" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="UrlIdentifier" />
</property>
</class>
</hibernate-mapping>
<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" name="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`WebpageMetaData`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="Webpage_id" />
<generator class="foreign">
<param name="property">Webpage</param>
</generator>
</id>
<one-to-one cascade="none" class="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="true" fetch="join" foreign-key="Webpage_id" name="Webpage" />
<property name="Keywords" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Keywords" />
</property>
</class>
</hibernate-mapping>