开发者

Reference an object from a column in another table

I have the following object model:

class ObjectA{
    public string Description {get;set;}
}

class ObjectB{
    public string Description {get;set;}
    public ObjectA A {get;set;}
}

class ObjectC{
    public string Description {get;set;}
    public ObjectB B {get;set;}
}

class ObjectD{
    public string Description {get;set;}
    public ObjectC C {get;set;}
    public ObjectA A {get;set;}
}

My tables are as follows:

A: ID(int), DESCRIPTION(string)

B: ID(int), DESCRIPTION(string), A_ID(int)

C: ID(int), DESCRIPTION(string), B_ID(int)

D: ID(int), DESCRIPTION(string), C_ID(int), A_ID(int)

In table D A_ID is 开发者_开发知识库a foreign key to the primary key of table A. This was originally done so that you could easily access ObjectA from ObjectD: ObjectD.A. This is causing the ID from table A to have to be added to every table that wants to access ObjectA in this manner de-normalizing the database (imagine having ObjectE, and ObjectF that all want to easily access ObjectA). In this case ObjectB will always have a reference to ObjectA.

I'd like to get rid of the reference to A_ID in all of my tables but I want to be able to easily access ObjectA from my classes. I know I could do ObjectD.ObjectC.ObjectB.ObjectA to get ObjectA when needed but that seems to defeat the purpose of lazy loading my objects.

How can I map my ObjectD so that I can have direct access to ObjectA without having ObjectA's ID in ObjectD's table?

Note: I'm using hbm/xml files to map my classes.


It seems to me you either live with the redundant data for performance reasons or you don't. I can't conceive of a third option. (But there are better NHibernate minds out there that may correct this view).

If the reads of these objects are much higher than the writes and the use cases require access to ObjectA most of the time you're working with an ObjectD then the redundancy is fine. Otherwise the ObjectD.C.B.A approach you refer starts to become more attractive.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜