title: Working with NHibernate and Oracle Sequences?
body: <blockquote>
    <p><strong>How to work with NHibernate and Oracle using Oracle Sequence?</strong></p>
  </blockquote>
  
  <p>When I call to the <code>SaveOrUpdate()</code> method, it only performs a select query against the Oracle sequence.</p>
  
  <p>I have used an interceptor to look up the query the session was performing, and the is the instruction I get:</p>
  
  <pre><code>select INFO_ACCESS_REQS_ID_SEQ.nextval from dual;
  </code></pre>
  
  <p>And that is the only thing being executed against the underlying Oracle database when I call to <code>ISession.SaveOrUpdate()</code>.</p>
  
  <blockquote>
    <p><strong><code>hibernate.cfg.xml</code></strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" &gt;
      &lt;session-factory name="MyProject"&gt;
          &lt;property name="connection.driver_class"&gt;
              NHibernate.Driver.OracleClientDriver
          &lt;/property&gt;
          &lt;property name="format_sql"&gt;true&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="dialect"&gt;NHibernate.Dialect.Oracle10gDialect&lt;/property&gt;
          &lt;property name="query.substitutions"&gt;
              true 1, false 0, yes 'Y', no 'N'
          &lt;/property&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <blockquote>
    <p><strong><code>InformationAccessRequest.hbm.xml</code></strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     namespace="MyProject.Model" 
                     assembly="MyProject"&gt;
      &lt;class name="InformationAccessRequest" table="INFO_ACCESS_REQS"&gt;
          &lt;id name="Id" column="INFO_ACCESS_REQS_ID"&gt;
              &lt;generator class="native"&gt;
                  &lt;param name="sequence"&gt;INFO_ACCESS_REQS_ID_SEQ&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="Assembly" column="ASSEMBLY_DT" /&gt;
          &lt;property name="Requested" column="INFO_ACCESS_REQ_DT" /&gt;
          &lt;property name="Expiration" column="INFO_ACCESS_REQ_EXP_DT" /&gt;
          &lt;property name="RequesterIdentification" column="INFO_ACCESS_REQ_USR_ID" /&gt;
          &lt;property name="Number" column="INFO_ACCESS_REQ_NUM" /&gt;
          &lt;property name="Reception" column="INFO_ACCESS_REQ_RECEP_DT" /&gt;
          &lt;property name="Creator" column="CREATOR_ID" /&gt;
          &lt;property name="Created" column="CREATED_DT" /&gt;
          &lt;property name="Updater" column="UPDATER_ID" /&gt;
          &lt;property name="Updated" column="UPDATED_DT" /&gt;
          &lt;property name="Deleted" column="DELETED_DT" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <blockquote>
    <p><strong><code>ISession.SaveOrUpdate()</code></strong></p>
  </blockquote>
  
  <pre><code>var newRequest = new InformationAccessRequest();
  newRequest.Requested = DateTime.Today.AddDays(-1);
  newRequest.Expiration = DateTime.Today.AddDays(1);
  newRequest.Reception = DateTime.Today;
  newRequest.Number = Guid.NewGuid().ToString().Substring(0, 12);
  newRequest.RequesterIndentification = Guid.NewGuid()..ToString().Substring(0, 10);
  newRequest.Created = DateTime.Today;
  newRequest.Creator = User.Current.Login;
  newRequest.IsNew = true;
  newRequest.IsDirty = true;
  session.SaveOrUpdate(newRequest);
  </code></pre>
  
  <p>All required information provided, and all database constraints respected. The SaveOrUpdate() only does the select against the sequence, and no other sql instruction gets performed.</p>