nhibernate/fluenthibernate throws StackOverflowException

Posted by Gianluca Colucci on Stack Overflow See other posts from Stack Overflow or by Gianluca Colucci
Published on 2010-05-19T17:00:33Z Indexed on 2010/05/20 0:10 UTC
Read the original article Hit count: 381

Hi there!

In my project I am using NHibernate/FluentNHibernate, and I am working with two entities, contracts and services.

This is my contract type:

[Serializable]
public partial class TTLCContract
{
    public virtual long? Id { get; set; }
    // other properties here
    public virtual Iesi.Collections.Generic.ISet<TTLCService> Services { get; set; }

    // implementation of Equals
    // and GetHashCode here
}

and this is my service type:

[Serializable]
public partial class TTLCService
{
    public virtual long? Id { get; set; }
    // other properties here
    public virtual Activity.Models.TTLCContract Contract { get; set; }

    // implementation of Equals
    // and GetHashCode here
}

Ok, so as you can see, I want my contract object to have many services, and each Service needs to have a reference to the parent Contract.

I am using FluentNhibernate. So my mappings file are the following:

public TTLCContractMapping()
{
    Table("tab_tlc_contracts");
    Id(x => x.Id, "tlc_contract_id");
    HasMany(x => x.Services)
            .Inverse()
            .Cascade.All()
            .KeyColumn("tlc_contract_id")
            .AsSet();
}

and

public TTLCServiceMapping()
{
     Table("tab_tlc_services");

     Id(x => x.Id, "tlc_service_id");
     References(x => x.Contract)
         .Not.Nullable()
         .Column("tlc_contract_id");
}

and here comes my problem: if I retrieve the list of all contracts in the db, it works. if I retrieve the list of all services in a given contract, I get a StackOverflowException....

Do you see anything wrong with what I wrote? Have I made any mistake?

Please let me know if you need any additional information.

Oh yes, I missed to say... looking at the stacktrace I see the system is loading all the services and then it is loading again the contracts related to those services.

I don't really have the necessary experience nor ideas anymore to understand what's going on.. so any help would be really really great!

Thanks in advance, Cheers, Gianluca.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate