Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing 'Assigned' ID with NHibernate
    text
    copied!<p>I have two entities, <strong>Job</strong> and <strong>Language</strong>, in a many-to-one relationship. The mapping configuration of these entities is as follows:</p> <pre><code>public class JobMap : ClassMap&lt;Job&gt; { public JobMap() { Table("\"JOB\""); LazyLoad(); Id(x =&gt; x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_job", "50"); Map(x =&gt; x.Name).Column("name").Unique(); References&lt;Language&gt;(x =&gt; x.SourceLanguage, "fk_id_language").Cascade.None(); } } public class LanguageMap : ClassMap&lt;Language&gt; { public LanguageMap() { Table("\"LANGUAGE\""); LazyLoad(); Id(x =&gt; x.Id, "id").GeneratedBy.Assigned().UnsavedValue(0); Map(x =&gt; x.Code).Column("code"); } } </code></pre> <p>I use this code to add a job into the database</p> <pre><code>private IServiceOperationResult AddJob(string jobName, Language sourceLanguage) { try { ISession session = sessionBuilder.GetSession(); using (ITransaction transaction = session.BeginTransaction(System.Data.IsolationLevel.Serializable)) { job = new Job(jobName, sourceLanguage); jobRepository.Add(job); transaction.Commit(); } } catch (Exception e) { log.Error("Error adding job to the DB", e); return new StringServiceOperationResult(e); } return new OkOperationResult(); } </code></pre> <p>The <strong>JobRepository.Add(Job job</strong>) is simple</p> <pre><code>public void Add(Job entity) { SessionBuilder.GetSession().Save(entity); } </code></pre> <p>The problem is that running this code with NHProfiler on, I can see the following warning before <strong>INSERT</strong> into the <strong>JOB</strong> table:</p> <blockquote> <p>WARN: Unable to determine if en-GB with assigned identifier 47 is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.</p> </blockquote> <p>followed by (unnecessary) <strong>SELECT</strong> from the <strong>LANGUAGE</strong> table to load the <strong>Language</strong> entity with the id 47 (where "en-GB" is a code of a <strong>Job.SourceLanguage</strong> object).</p> <p>In my case I do not want to ever update, insert or delete a <strong>Language</strong> entity from the database (in situation where it is just referenced from other entities), it is really immutable table, filled with the correct rows all the time. Is there a way to tell NHibernate to just take the <strong>Language</strong> from me and when inserting into the <strong>JOB</strong> table, to use its <strong>Id</strong> property and not care whether it is present in the database or not?</p> <p>When I add this line before adding the <strong>Job</strong> to the repository</p> <pre><code>job.SourceLanguage = session.Load&lt;Language&gt;(sourceLanguage.Id); </code></pre> <p>I no longer get the warning nor the <strong>SELECT</strong>, but it is kind of cumbersome to do this everywhere I work with the <strong>Language</strong> entity.</p> <p>Thank you very much.</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