Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate Mapping - Saving Child object
    text
    copied!<p>I'm having a problem with Fluent NHibernate mappings, I think, and can't quite get past how I should be setting the mapping to avoid an issue.</p> <p>I have a business object (literally, a "business"), and a review object. Each Business can have multiple reviews that are created on a page in the UI. The business is a property of the Review, as follows:</p> <pre><code>public class Business { public virtual int BusinessId {get;set;} public virtual DateTime LastModified {get;set;} public virtual IList&lt;Review&gt; Reviews {get;set;} [... more removed for brevity ...] } public class Review { public virtual int ReviewId {get;set;} public virtual string ReviewText {get;set;} public virtual Business Business {get;set;} [... more removed for brevity ...] } </code></pre> <p>My mappings are as follows:</p> <pre><code>public class ReviewMap : ClassMap&lt;Review&gt; { public ReviewMap() { WithTable("Reviews"); Id(x =&gt; x.ReviewId).TheColumnNameIs("ReviewId").GeneratedBy.Identity(); References(x =&gt; x.Business).TheColumnNameIs("BusinessId"); Map(x =&gt; x.ReviewText); [... and so on...] } public class BusinessMap : ClassMap&lt;Business&gt; { public BusinessMap() { WithTable("Businesses"); Id(x =&gt; x.BusinessId).TheColumnNameIs("BusinessId").GeneratedBy.Identity(); Map(x =&gt; x.Name).TheColumnNameIs("BusinessName"); Map(x =&gt; x.LastModified, "LastModifiedOn"); HasMany&lt;Review&gt;(x =&gt; x.Reviews) .Inverse() .LazyLoad(); [... more removed for brevity ...] } } </code></pre> <p>The repository code is </p> <pre><code>public void Save(T entity) { using (ISession session = GetSession()) using (ITransaction tx = session.BeginTransaction()) { session.SaveOrUpdate(entity); tx.Commit(); } } </code></pre> <p>In the code, I assign the properties to the Review object, and call the Repository's Save method. </p> <p>The problem is that since I'm not updating the Business per se, I don't expect it to get saved--all I want is the review saved. But the code tries to save the Business as well, and I get an excption, as I haven't set the "LastModified" property--nor do I want to, as I'm saving the REVIEW, not the business.</p> <p>How should I be setting up the mapping to let this happen? </p>
 

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