How to determine one to many column name from entity type

Posted by snicker on Stack Overflow See other posts from Stack Overflow or by snicker
Published on 2010-04-21T18:13:06Z Indexed on 2010/04/22 9:13 UTC
Read the original article Hit count: 269

I need a way to determine the name of the column used by NHibernate to join one-to-many collections from the collected entity's type.

I need to be able to determine this at runtime.

Here is an example: I have some entities:

namespace Entities
{
    public class Stable {
        public virtual int Id {get; set;}
        public virtual string StableName {get; set;}
        public virtual IList<Pony> Ponies { get; set; }
    }

    public class Dude {
        public virtual int Id { get; set; }
        public virtual string DudesName { get; set; }
        public virtual IList<Pony> PoniesThatBelongToDude { get; set; }
    }

    public class Pony {
        public virtual int Id {get; set;}
        public virtual string Name {get; set;}
        public virtual string Color { get; set; }
    }
}

I am using NHibernate to generate the database schema, which comes out looking like this:

create table "Stable" (Id  integer, StableName TEXT, primary key (Id))
create table "Dude" (Id  integer, DudesName TEXT, primary key (Id))
create table "Pony" (Id  integer, Name TEXT, Color TEXT, Stable_id INTEGER, Dude_id INTEGER, primary key (Id))

Given that I have a Pony entity in my code, I need to be able to find out:

  • A. Does Pony even belong to a collection in the mapping?
  • B. If it does, what are the column names in the database table that pertain to collections

In the above instance, I would like to see that Pony has two collection columns, Stable_id and Dude_id.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate