I'm pretty new to Nhibernate, so apologies for a long - winded description
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.
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).
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.
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..), then jumps back to start of the method, and keeps on doing this until it raises a stack overflow.
Here's my generator class
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');
}
}
Here's my mapping foe the EndPoint CLass
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DataClasses"
namespace="DataClasses">
<class name="EndPoint" table="[Addresses_Contacts]">
<composite-id>
<key-property name ="Address" column ="[Address ID]" type="string" />
<key-property name ="Contact" column ="[Contact ID]" type="string"/>
</composite-id>
<many-to-one name="Address" class="DataClasses.Address, DataClasses" cascade="save-update"/>
<many-to-one name="Contact" class="DataClasses.Contact, DataClasses" cascade="save-update"/>
</class>
</hibernate-mapping>
and the mapping for address:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataClasses" namespace="DataClasses">
<class name="Address" table="[Lookup Addresses]" >
<id name="Code" column="ID" type="string">
<generator class="Nhibernate.AddressIdGenerator, Nhibernate" />
</id>
<property name="OrganisationName" column="[Name of Organisation]"/>
<property name="StreetAddress1" column="[Park/centre/estate]" />
<property name="StreetAddress2" column="[Street Name]" />
<property name="Town" column="[Town/City]" />
<property name="State" column="[Region/ State]" />
<property name="PostCode" column="[Postal/ Area Code]" />
<property name="District" column="[Local District]" />
<property name="Airport" column="[Airport code]" />
<many-to-one name="Country" class="DataClasses.Country, DataClasses" column ="[Country Code]"/>
</class>
</hibernate-mapping>
If I try to save and Address on its own, it works fine, the ID is generated with no problems.
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.
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.