views:

210

answers:

1

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.

A: 

i'm thinking that the problem resides that you are doing an nh-query inside the Generator and you are querying the entity type you want to save.

The generator is called not when you call Save() but whenever there is a need to flush/commit the data. Now, Save() places the entity on a action-queue of things to do. When you call CreateQuery/CreateCriteria and request the result via List()/UniqueResult() nhibernate's engine detects that you made a Save() request on an entity and so will try to flush/commit the entity first (and thus call the generator) and then perform the query and thus start an infinite loop; The logic is such that since you called Save() and then you are querying that class type you will want the result set to include the Saved object.

So, replace your Nh-query with a native SqlCommand (there is a possibility CreateSqlQuery will work too) and i think your problem will be solved.

Jaguar