title: Stack Overflow whilst cascade saving nhibernate entity with custom generator
body: <p>I'm pretty new to Nhibernate, so apologies for a long - winded description</p>
  
  <p>I suspect that changing the structure of the legacy DB is probably the best option, but I want to try and get NHibernate to deal with it.</p>
  
  <p>Basically the structure is this an EndPoint has an address and a contact. Endpoint is stored in a table with a composite ID (Address ID, Contact ID).</p>
  
  <p>I'm having a problem when cascade saving an address, which has a custom ID generator - address ID are of the form "ADR000234" to fit in with a legacy DB structure.</p>
  
  <p>The custom ID generator includes a query, and when I save the address as part of an endpoint, I get a stack overflow. When debugging the cursor gets to line that evaluates the query( var maxAddressID..), <strong>then jumps back to start of the method</strong>, and keeps on doing this until it raises a stack overflow.</p>
  
  <p>Here's my generator class</p>
  
  <pre><code>public class AddressIdGenerator :  IIdentifierGenerator
  {
      public object Generate(ISessionImplementor session, object obj)
      {
          var castAsSession = (ISession)session;
          var allAddresses = castAsSession.CreateQuery("select max(Code) from Address a");
          var maxAddressID = (string)allAddresses.List()[0];
          var previousNumber = int.Parse(maxAddressID.Substring(3, 6));
  
          return GetNewId("ADR", previousNumber);
      }
  
      private string GetNewId(string prefix, int number)
      {
          return prefix + (number + 1).ToString().PadLeft(6, '0');
      }
  }
  </code></pre>
  
  <p>Here's my mapping foe the EndPoint CLass</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
      &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
      assembly="DataClasses"
      namespace="DataClasses"&gt;
      &lt;class name="EndPoint" table="[Addresses_Contacts]"&gt;
      &lt;composite-id&gt;
      &lt;key-property name ="Address" column ="[Address ID]" type="string" /&gt;
      &lt;key-property name ="Contact" column ="[Contact ID]" type="string"/&gt;
      &lt;/composite-id&gt;
  
      &lt;many-to-one name="Address" class="DataClasses.Address, DataClasses" cascade="save-update"/&gt;
      &lt;many-to-one name="Contact" class="DataClasses.Contact, DataClasses" cascade="save-update"/&gt;
  
      &lt;/class&gt;
      &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>and the mapping for address:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataClasses" namespace="DataClasses"&gt;
  &lt;class name="Address" table="[Lookup Addresses]" &gt;
  &lt;id name="Code" column="ID" type="string"&gt;
    &lt;generator class="Nhibernate.AddressIdGenerator, Nhibernate" /&gt;
  &lt;/id&gt;
  &lt;property name="OrganisationName" column="[Name of Organisation]"/&gt;
  &lt;property name="StreetAddress1" column="[Park/centre/estate]" /&gt;
  &lt;property name="StreetAddress2" column="[Street Name]" /&gt;
  &lt;property name="Town" column="[Town/City]" /&gt;
  &lt;property name="State" column="[Region/ State]" /&gt;
  &lt;property name="PostCode" column="[Postal/ Area Code]" /&gt;
  &lt;property name="District" column="[Local District]" /&gt;
  &lt;property name="Airport" column="[Airport code]" /&gt;
  &lt;many-to-one name="Country" class="DataClasses.Country, DataClasses" column ="[Country Code]"/&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>If I try to save and Address on its own, it works fine, the ID is generated with no problems.</p>
  
  <p>Also if I remove the Address and Contact properties from the mapping (but not from the composite ID), and save the Address and Contact before saving the Endpoint, it's fine too.</p>
  
  <p>It seems to me that when I'm doing the cascade save, for some reason it can't run other queries during the process, but rather than throwing an exception it's behaving strangely (restarting the method again and again). I haven't ever seen a C# method do this before. I'd love to know if anyone has an idea of how to fix this.</p>