Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with mapping in NHibernate
    primarykey
    data
    text
    <p>I've the following class:</p> <pre><code>public class Customer { public virtual int CustomerID { get; protected set; } public virtual string AccountNumber { get; protected set; } public virtual string CustomerType { get; set; } public virtual int TerritoryID { get; set; } public virtual SalesTerritory Territory { get; protected set; } } </code></pre> <p>And it is being mapped using Fluent NHibernate as follows:</p> <pre><code> public class CustomerMapper : ClassMap&lt;Customer&gt; { private const string schema = "Sales"; public CustomerMapper() { SchemaIs(schema); Id(x =&gt; x.CustomerID); Map(x =&gt; x.CustomerType).WithLengthOf(1).Not.Nullable(); Map(x =&gt; x.TerritoryID).Not.Nullable(); Map(x =&gt; x.AccountNumber).ReadOnly() .Update(false) .Insert(false) .Generated(Generated.Insert); References&lt;SalesTerritory&gt;(x =&gt; x.Territory).TheColumnNameIs(ReflectionHelper.GetProperty&lt;Customer&gt;(x =&gt; x.TerritoryID).Name).LazyLoad().Update(false).Insert(false); } } </code></pre> <p>The Update and Insert methods are just extension methods that set the <strong><em>update</em></strong> and <strong><em>insert</em></strong> attributes of a property in the generated mapping file.</p> <p>This is how the resulting mapping file looks:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="Model" namespace="Model" schema="Sales"&gt; &lt;class name="Customer" table="`Customer`" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;id name="CustomerID" column="CustomerID" type="Int32"&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="AccountNumber" column="AccountNumber" insert="false" update="false" generated="insert" length="100" type="String"&gt; &lt;column name="AccountNumber" /&gt; &lt;/property&gt; &lt;property name="TerritoryID" column="TerritoryID" not-null="true" type="Int32"&gt; &lt;column name="TerritoryID" /&gt; &lt;/property&gt; &lt;property name="CustomerType" column="CustomerType" not-null="true" length="1" type="String"&gt; &lt;column name="CustomerType" /&gt; &lt;/property&gt; &lt;many-to-one lazy="proxy" update="false" insert="false" name="Territory" column="TerritoryID" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>My problem is that when a save a new customer instance into the database, the customer instance have its Territory property as null.<br/> How can I make this property to contain the correct value after saving into the database? I am looking at something like the <strong><em>generated</em></strong> attribute for properties which retrieve the correct value after the session is flushed.<br/> Or is there something wrong with my Customer class? <br/> Thanks in advance.</p> <p><strong>EDIT 1:</strong> The database that I'm using for this model is AdventureWorks (SQL2005), tables Sales.Customer and Sales.SalesTerritory.</p>
    singulars
    1. This table or related slice is empty.
    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