Nhibernate:在相同的两个实体之间映射两个不同的属性
我有一个 A 类:
public class ClassA
{
public int ID {get; private set;}
public string Code {get; private set;}
public ClassB B {get; private set;}
public IList<ClassB> ListB {get; private set;}
}
和一个 B 类:
public class ClassB
{
public int ID {get; private set;}
public string Code {get; private set;}
public ClassA A {get; private set;}
//some other attributes...
}
以及映射:
public ClassAMap()
{
Table("ClassA");
Id(classA => classA .ID, "ID").GeneratedBy.Identity();
Map(classA => classA.Code, "Code").Unique().Not.Nullable();
//HERE IS THE PROBLEM: --------
References(classA => classA.B,"IDClassB").Cascade.SaveUpdate();
//-----
HasMany(classA => classA.ListB).Table("ClassB").KeyColumn("IDClassA").AsBag().Not.LazyLoad().Inverse().Cascade.AllDeleteOrphan();
}
B 类映射:
public ClassBMap()
{
Table("ClassB");
Id(classB => classB.ID).GeneratedBy.Identity();
References(classB => classB.A, "IDClassA").ForeignKey("ID").Cascade.SaveUpdate();
}
A 类中 ListB 的映射工作正常,因为起初只有 ListB 属性而不是 B,当我必须映射 B 时,我尝试了以下操作:
References(classA => classA.B,"IDClassB");
映射测试失败,因为 B 没有保存,所以我这样做了:
References(classA => classA.B,"IDClassB").Cascade.SaveUpdate();
这次保存了 B,但是通过保存 B,classA 被 AB 和 BA 插入了两次
我该如何解决这个问题?为什么它适用于 ListB 属性而不适用于 B 属性?谢谢
I have a Class A:
public class ClassA
{
public int ID {get; private set;}
public string Code {get; private set;}
public ClassB B {get; private set;}
public IList<ClassB> ListB {get; private set;}
}
And a ClassB:
public class ClassB
{
public int ID {get; private set;}
public string Code {get; private set;}
public ClassA A {get; private set;}
//some other attributes...
}
And the Mappings:
public ClassAMap()
{
Table("ClassA");
Id(classA => classA .ID, "ID").GeneratedBy.Identity();
Map(classA => classA.Code, "Code").Unique().Not.Nullable();
//HERE IS THE PROBLEM: --------
References(classA => classA.B,"IDClassB").Cascade.SaveUpdate();
//-----
HasMany(classA => classA.ListB).Table("ClassB").KeyColumn("IDClassA").AsBag().Not.LazyLoad().Inverse().Cascade.AllDeleteOrphan();
}
ClassB Mappings:
public ClassBMap()
{
Table("ClassB");
Id(classB => classB.ID).GeneratedBy.Identity();
References(classB => classB.A, "IDClassA").ForeignKey("ID").Cascade.SaveUpdate();
}
The mappings for ListB in classA worked ok, because at first the was only ListB property and not B, when i had to map B i tried this:
References(classA => classA.B,"IDClassB");
The mapping test failed because B wasn't saved, so i did this:
References(classA => classA.B,"IDClassB").Cascade.SaveUpdate();
This time B was saved, but by saving B, classA was inserted two times, by A.B and by B.A.
How can i solve this problem? Why does it work for the ListB property and not for the B property? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在 classA 中创建一个包含 B 和 listB 元素的列表来解决这个问题,属性 B 和 listB 现在是该新列表的“视图”。为了区分这个新列表中以前的 listB 元素和以前的 B 属性,我在 classB 中添加了一个布尔值,如果 true 就是 B,如果 false 就是 listB 元素。
I solved this by creating a list in classA that contains B and listB elements, the properties B and listB now are "views" over that new list. To distinguish former listB elements from former B properties in this new list, i added a boolean in classB, if true is B is false is listB element.