NHibernate映射按代码多对一与中间表
本文关键字:中间 多对一 代码 映射 NHibernate | 更新日期: 2023-09-27 17:57:46
这是一个场景。我有两个类,比如丈夫和妻子:D,这两个类之间的关系是通过第三个名为"人"的中间表来定义的。
类别:
class Husband
{
public virtual int HusbandId { get; set; }
public virtual Wife Wife { get; set; }
}
class Wife
{
public virtual int WifeId { get; set; }
...
}
表格:
Husband :: Table
HusbandId : int
Wife :: Table
WifeId : int
People :: Table
PeopleId : int
ManId : int
WomanId : int
RelationType : int
在People表中,RelationType=1表示一个男人和一个女人之间的婚姻关系,其中ManId==HusbandId,WomanId==WifeId。
请注意,保证"人物"表中每个丈夫只有一个妻子。此外,不用说,我不能修改这些表格。它是遗留数据库。
映射:
class HusbandMap : ClassMapping<Husband>
{
public HusbandMap()
{
Id(x => x.HusbandId);
ManyToOne(x => x.Wife); // <-- How to make this mapping work ?
}
}
class WifeMap : ClassMapping<Wife>
{
public WifeMap()
{
Id(x => x.WifeId);
}
}
现在的问题是,我如何使用中间表People来进行从丈夫到妻子的多对一映射?
到目前为止,我发现了一个非常糟糕的解决方案,它可以工作,但它只是糟糕的:D
class HusbandMap : ClassMapping<Husband>
{
public HusbandMap()
{
Id(x => x.HusbandId);
Join("People", j =>
{
j.Key(x => x.Column("manId and relationType = 1"); //here is the sql injection hack :D
j.ManyToOne(x => x.Wife);
});
}
}
我面临的问题是,我找不到任何其他方法将relationType = 1
推送到生成的sql中。有人知道怎么做吗?