views:

33

answers:

1

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.

A: 

Since Pony is created with the Stable_id and Dude_id I'm assuming that you have the appropriate mapping in the hbm-xml.

You could also add entities in Pony for Stable and Dude to further clarify the relationship.

jishi