开发者

How to determine one to many column name from entity type

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开发者_JAVA技巧 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜